views:

582

answers:

3

FILE:

hello

world

foo

bar

How can when remove all the empty new lines in this FILE?

Output of command:

FILE:

hello
world
foo
bar
+7  A: 

grep . FILE


(And if you really want to do it in sed, then: sed -e /^$/d FILE)

(And if you really want to do it in awk, then: awk /./{print} FILE)

DigitalRoss
as simple as it gets
Brian Rasmussen
Sweet! TY for the other commands as well - but looks like grep is my new best friend.
grep . FILE doesn't work for me. It's probably better to stick with grep for searching file contents, and sed for editing file contents.
Michael Dillon
a minor "issue" with grep . FILE is that if a space represents and blank line, then grep will grab that blank line as well.
ghostdog74
`sed -ne/./p` works too, and `awk /./` is shorter (action is `{print}` if left unspecified). @ghostdog74: `grep '[^[:space:]]'` then.
ephemient
A: 

Or   grep -v -e '^$'

Mr.Ree
this has the same effect as grep . FILE in that a space as a blank line will get grabbed.
ghostdog74
"grep" looks for any line that matches the pattern. "." matches any character. "grep . FILE" matches any line with at least 1 character. Whereas "grep -v" excludes lines matching the pattern. OP said "remove all the empty new lines". If you want to exclude lines with just spaces, "grep -v '^ *$'". The "*" will match zero or more of the preceding pattern, in this case a space. Though you might prefer to match and exclude other whitespace characters (tabs, form-feeds, etc) too.
Mr.Ree
BTW: "^" matches beginning of line. "$" matches the end of line.
Mr.Ree
+2  A: 
with awk, just check for number of fields. no need regex

$ more file
hello

world

foo

bar

$ awk 'NF' file
hello
world
foo
bar
ghostdog74
Doesn't need quotes. This trick is found in `awk1line.txt` -- then again, so are most awk tricks :)
ephemient
its just my good practice to put quotes, since you are running it from shell.. for composite awk statements, you still have to put quotes. so why not cultivate this habit.
ghostdog74