LINUX – Remove empty lines – remove double returns

with sed find a line that starts with an ending char (a blank line) and delete it

with sed find a line that starts with an ending char (a blank line) and replace it with a nothing (the thing between the last 2 slashes is what it replaces with, and its a nothing)


What if there are spaces and tabs (white spaces in the line) that a human would think its a blank line but a computer with the above scripts would not count them as a match so it wouldnt delete em. Well this is how you delete a line with whitespace only in it:

With Grep:

With grep a simple dot . can match anything. The easiest one is . as it matches a single character to anything.



With Awk:

Likewise with awk you can do the same thing. All search terms in awk are put in // just like with sed (but sed operates with deletes or replaces etc.) With awk you put a search term and usually a small awk mini program can operate on what it found (it can operate a small awk mini script on a line that contains what it found) if you put the program as print $0 then it just print that line and behaves as a grep. The regexp goes between the // and the print $0 will print the whole line. Another trick to print the whole line is to not even put a mini program. Usually the syntax is like so /regexp/{mini program}. But if you avoid the {miniprogram} it just prints out the line that has /regexp/ found in it. Well what if you set the regexp to be a dot just like in grep which finds to anything besides an empty line? Well you get a command that simply removes blank lines


Leave a Reply

Your email address will not be published. Required fields are marked *