View Full Version : [SOLVED] Remove all empty lines in file using grep?
hakermania
April 10th, 2011, 05:43 PM
I know that
grep -v "\here_something" file
like
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!
Vaphell
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*$'
ghostdog74
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
bashologist
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.
http://sed.sourceforge.net/sed1line.txt
kurum!
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
bashologist
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
continue
fi
echo "$line"
done < file.txt
geirha
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
kurum!
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.
bashologist
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"
fi
done < testing.txt
I don't think you can beat that. :)
hakermania
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 :)
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.