I used sed '/pattern/d' file > newfile
to remove some lines in a text file, but there are lots of blank lines left in the new file.
How can I modify the command to avoid the blank lines?
I used sed '/pattern/d' file > newfile
to remove some lines in a text file, but there are lots of blank lines left in the new file.
How can I modify the command to avoid the blank lines?
sed '/pattern/d; /^$/d' file > newfile
There is some good discussion about regular expressions for deleting empty lines in a file in this Stack Overflow post
The command that you use will delete all the lines you want to delete, but it leaves the blank lines that are already there in the original file in place. To delete these too, simply apply a second filter to the input:
$ sed -e '/pattern/d' -e '/^[:blank:]*$/d' <file >newfile
The second filter will remove lines that are empty, or that contains only whitespace characters (i.e., that are "blank" in their appearance).