Home > Blockchain >  UNIX: using tr to delete empty lines
UNIX: using tr to delete empty lines

Time:01-07

I've seen it being done using sed (if you'd be so kind to write how to do it in sed and explain the regex behind it, I will appreciate it too), but I'd like to know how to do it using tr.

My idea was: cat file|tr -d ^'\n' or ^\n, but the first one deleted every '\n', the second one none.

CodePudding user response:

tr on linux, at least, can squeeze repeated characters:

echo -ne $a
the quick
brown fox

jumps over
echo -ne $a |tr -s '\n'
the quick
brown fox
jumps over

CodePudding user response:

tr operates on streams of characters, not streams of lines (which is the default for sed and awk and whatnot). The -d flag deletes any of the characters in the character list. tr -d '\n' will delete all the newlines in the stream.

I'm not sure myself if you can delete empty lines using tr.

To use sed sed '/^$/d'. ^ matches front of line. $ matches end of line d says you want sed to delete anything that matches that. However, this type of question has been answered much more comprehensively elsewhere so I'd go back to google and find a comprehensive sed or unix tutorial.

CodePudding user response:

Another solution is grep . file.

  •  Tags:  
  • Related