Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Delete blanks lines in a text plain file

Highlighted
Occasional Contributor

Delete blanks lines in a text plain file

Hi everyone...
You know, I've been looking for any kind of command with sed or awk for deleting blanks lines in a txt file on a HP-UX server.
I already got the filtered file and I found a web page in which they say with the expression "sed '/^$/d'" but this isn´t working for me.
Can anybody help me with this?
Thanks a lot!
10 REPLIES
Honored Contributor Honored Contributor

Re: Delete blanks lines in a text plain file

This works for me..

sed /^$/d my.file

post your error, maybe that will help ?


Acclaimed Contributor Acclaimed Contributor

Re: Delete blanks lines in a text plain file

Hi:

# perl -pi.old -e 's/^\s*$//' file

...will eliminate all blank lines from your file after creating a backup copy suffixed with ".old".

This works for lines that consist only of a newline or lines that have leading spaces and/or tab characters only.

Regards!

...JRF...
Honored Contributor Honored Contributor

Re: Delete blanks lines in a text plain file

Hi,

# sed '/^$/d' testfile # Delete blank Line from a file using sed
Acclaimed Contributor Acclaimed Contributor

Re: Delete blanks lines in a text plain file

Hi (again):

To be clear:

# sed /^$/d file

...will *not* delete "blank" lines unless those lines consist *only* of a newline character --- without leading whitespace.

The Perl snippet I suggested works both for lines that consist only of a newline or lines that have leading spaces and/or tab characters only before the final newline character.

If you insist on 'sed', this mimics the Perl snippet albeit without the inplace file modication:

# sed -e '/^[ ]*$/d' file

Notice carefully that the characters between the square brackets are really a blank and a tab. HP-UX's 'sed' doesn't allow metacharacters like '\t' to a tab. HP-UX's 'sed' doesn't allow inplace modification, either.

Regards!

...JRF...
Honored Contributor Honored Contributor

Re: Delete blanks lines in a text plain file

This will work as well:

sed '/^[[:space:]]*$/d'

And it has the benefit of being easier to read than the brackets with a and a character between them.
Honored Contributor Honored Contributor

Re: Delete blanks lines in a text plain file

The shortest solution:

awk NF data.txt

This handles zero length lines, spaces and tabs -- any number on the blank lines.
Honored Contributor Honored Contributor

Re: Delete blanks lines in a text plain file

Hi Sergio,

>> any kind of command with sed or awk
You got great answers for sed/awk. In case you look for anything else, try this:

# more filename | grep -v ^$

Regds..
Don't fix that what ain't broke
Acclaimed Contributor Acclaimed Contributor

Re: Delete blanks lines in a text plain file

>RK: more filename | grep -v ^$

Not sure why you want more(1) involved?
(Unless you want to use -s to give grep a head start? :-)
Just use: grep -v ^$ filename
Or: grep -v '^[[:space:]]*$' filename
Honored Contributor Honored Contributor

Re: Delete blanks lines in a text plain file

>># more filename | grep -v ^$

This will have the same problem as the initial 'sed' statement posted. It will only match lines that consist of a "newline" character ONLY. If there are spaces and/or tabs on the line that will not work.

To take care of spaces and/or tabs you would have to do:

# more filename | grep -v ^[[:space:]]*$

or, even easier with spawning a unnecessary "more" process:

# grep -v ^[[:space:]]*$ filename
Honored Contributor Honored Contributor

Re: Delete blanks lines in a text plain file

Thanks Dennis and Patrick, I will surely go through these differences.
Don't fix that what ain't broke