Delete blanks lines in a text plain file (311 Views)
Reply
Occasional Contributor
Sergio Bascur
Posts: 5
Registered: ‎04-10-2007
Message 1 of 11 (311 Views)

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!
Honored Contributor
Tim Nelson
Posts: 3,727
Registered: ‎06-01-2000
Message 2 of 11 (311 Views)

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
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 11 (311 Views)

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
Hakki Aydin Ucar
Posts: 1,313
Registered: ‎08-16-2006
Message 4 of 11 (311 Views)

Re: Delete blanks lines in a text plain file

Hi,

# sed '/^$/d' testfile # Delete blank Line from a file using sed
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 11 (311 Views)

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
Patrick Wallek
Posts: 13,772
Registered: ‎06-21-2000
Message 6 of 11 (311 Views)

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
Bill Hassell
Posts: 14,205
Registered: ‎05-29-2000
Message 7 of 11 (311 Views)

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
R.K. #
Posts: 705
Registered: ‎05-20-2008
Message 8 of 11 (311 Views)

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
Dennis Handly
Posts: 25,058
Registered: ‎03-06-2006
Message 9 of 11 (311 Views)

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
Patrick Wallek
Posts: 13,772
Registered: ‎06-21-2000
Message 10 of 11 (311 Views)

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
R.K. #
Posts: 705
Registered: ‎05-20-2008
Message 11 of 11 (311 Views)

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
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.