views:

40

answers:

4

I tried grep -v '^$' in Linux and that didn't work. This file came from a Windows file system.

+2  A: 

Try the following:

grep -e "^$" -v foo.txt

The -e option allows regex patterns for matching.

UPDATE: This works for me for a file with blank lines or white spaces on windows:

egrep -v "^[[:space:]]?$" foo.txt
ars
It's still showing blank lines.
awakeFromNib
See update to the question.
ars
Yeah that works. It also works with grep.
awakeFromNib
A: 

I prefer using egrep, though in my test with a genuine file with blank line your approach worked fine (though without quotation marks in my test). This worked too:

egrep -v "^(\r?\n)?$" filename.txt
chryss
Tried that. Blank lines are still showing. Could this be because the file was made in Windows?
awakeFromNib
+1  A: 

Tried hard but this seems to work (assuming \r is biting you here)

printf "\r" | egrep -xv "[[:space:]]*"
mvds
That works if I replace the first part with output from the file.
awakeFromNib
A: 
$ dos2unix file 
$ grep -v "^$" file

Or just simply awk

awk 'NF' file

If you don't have dos2unix, then you can use tools like tr

tr -d '\r' < "$file" > t ; mv t "$file"
ghostdog74
Can't find the program dos2unix. Is that for Windows? the ask command doesn't work either.
awakeFromNib