View Full Version : [SOLVED] Remove all empty lines in file using grep?

April 10th, 2011, 05:43 PM
I know that
grep -v "\here_something" file

grep -v "\@" file
will remove all lines starting with @
But I've tried \n instead of @ and (as I supposed) it doesn't work.
It is important for me the solution to be with grep and not with awk or sed, because I want to remember the solution and not to combine them to my mind and then forget all I knew :P

Thx in advance!

April 10th, 2011, 06:40 PM
grep -Ev '^$' file
or when you need to get rid of whitespace-only lines too, upgrade regex to '^\s*$'

April 11th, 2011, 01:04 AM
Use awk. It has the advantage of getting rid of empty lines for you AND if you need other processing on other lines, it will do that for you too.

#remove empty lines
awk 'NF' file

for example, if you need to count how many lines that are not empty

awk 'NF{c++}END{print c}' file

April 11th, 2011, 02:02 AM
Another example using perl. This one removes any line containing just white space. Redirect the output or use the i switch to overwrite file.

perl -n -e 'print unless /^\s*$/' file.txt

Sed also works great check out this page for lots of great examples.


April 11th, 2011, 02:16 AM
Similarly with Ruby(1.9+)

remove lines also with just white space

ruby -ne 'print unless /^\s*$/' file

remove empty lines

ruby -ne 'print unless /^$/' file

April 11th, 2011, 02:34 AM
Thats interesting. Ruby looks just like perl lol.

How about this bash example....

while read line; do
if [[ "$line" =~ ^$ ]]; then
echo "$line"
done < file.txt

April 11th, 2011, 08:16 AM
To edit files, use an editor...

ed -s file <<< $'g/^$/d\nw'

@bashologist, that bash loop will also omit lines containing only whitespace. And using a regex for that is a bit overkill imo. I'd do:

while IFS= read -r line; do
[[ $line ]] && printf '%s\n' "$line"
done < file

April 11th, 2011, 08:16 AM
Thats interesting. Ruby looks just like perl lol.

Ruby borrows syntax from Perl and also from Python. Its got the best of both worlds.

April 12th, 2011, 08:16 PM
@geirha Looks complex. I don't wanna use the brain power nessicary to process this ifs read commands.

But I have maybe a better solution if white space isn't an issue.

while read input; do
if [ -n "$input" ]; then
echo "$input"
done < testing.txt

I don't think you can beat that. :)

April 12th, 2011, 08:31 PM
Omg guys! Too effort so just a simple question!
Do not solve a problem that it's already solved?
Thx all for your effort anyway :) I love UF :)