Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

UNIX: using tr to delete empty lines

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading