02-18-2010 12:16 PM
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!
02-18-2010 12:48 PM
# 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.
02-18-2010 01:32 PM
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.
02-18-2010 07:24 PM
>> 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 ^$
02-18-2010 07:34 PM
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
02-18-2010 07:37 PM
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