I have a bunch of text files, some of which contain lines that are empty, i.e. only consist only of a newline, or possible spaces followed by a newline. I locate the files using a find
command.
- Example file
#Title 1 12345678 1234 #Title 2 12345678 1234 12345678 1234
- Expected output
#Title 1 12345678 1234 #Title 2 12345678 1234 12345678 1234
I want to remove all such empty lines. I tried it with the following command on Debian Linux Stretch:
cat "/path/to/file" | sed '/^\s*$/d' | sponge "/path/to/file";
Some of the files had for example 4 or more trailing blank lines, but the above command only removed all but one of the trailing blank lines.
How could I remove this last trailing blank line? As mentioned: should there be any blank lines further up in the file then these should be removed also.
I am trying to get some consistency between the files as the files are stored in an array of sorts within a BASH variable. The files are then looped over and all the blank lines and trailing blank lines are removed, while some of the files already don't have blank lines or any trailing blank line.
sed
versions have a-i
flag that allows changing the file in place, so you won't need pipes, just:sed -i '/^\s*$/d' /path/to/file
.s
characters. The lines that are not removed may contain spaces.