Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove empty lines in a text file via grep

FILE:

hello  world  foo  bar 

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

Output of command:

FILE:

hello world foo bar 
like image 884
user191960 Avatar asked Oct 23 '09 07:10

user191960


People also ask

How do I remove blank lines from grep?

By Using [: blank:] Keyword To create a file on the terminal, including the following commands. Text written in these files contains spaces between them, as seen in the figure below. These blank lines can be removed using a blank command to ignore empty spaces between the words or strings.

How do I remove blank lines from awk?

We can remove blank lines using awk: $ awk NF < myfile.


2 Answers

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 /./ FILE)

like image 53
DigitalRoss Avatar answered Sep 19 '22 00:09

DigitalRoss


Try the following:

grep -v -e '^$' 
like image 26
Mr.Ree Avatar answered Sep 20 '22 00:09

Mr.Ree