Operating System - HP-UX
1832983 Members
3210 Online
110048 Solutions
New Discussion

Getting rid of blank line

 
khilari
Regular Advisor

Getting rid of blank line

Hi guys i want to get rid of a blank line in the output of a file... Lets say i have an output

sp02 97216 Mbytes allocated to /dev/vgDEVsap across 7 PVs. PE size is 16 Mbyes, total of 6076 PE with 723 free

sp02 97216 Mbytes allocated to /dev/vgDEVsap across 7 PVs. PE size is 16 Mbyes, total of 6076 PE with 723 free

sp02 236096 Mbytes allocated to /dev/vgDEVdata across 17 PVs. PE size is 16
bytes, total of 14756 PE with 0 free

Now, i am getting this blank line after each entry that i want to get rid of.. Let me know what you think.
Thanks
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor

Re: Getting rid of blank line

Hi:

One way:

# perl -pe 's/^\s*$//' file

...and if you want to update in place :

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

...which will create a backup of the input file as 'file.old'. This technique eliminates have to redirect to a temporary file and then overwrite your original file with the temporary one.

Regards!

...JRF...
Pete Randall
Outstanding Contributor

Re: Getting rid of blank line

Or, with sed:

sed '/^$/d'


Pete

Pete
Ganesan R
Honored Contributor

Re: Getting rid of blank line

Hi Khilari,

You can easily get rid out of this using simple grep.

#grep -v ^$ filename > newfilename

Hope this helps.
Best wishes,

Ganesh.
James R. Ferguson
Acclaimed Contributor

Re: Getting rid of blank line

Hi (again):

Unfortunately, using the regular expresssion (RE):

/^$/

...only rids the file of lines that consist solely of a newline. That is, any leading spaces and/or tabs will cause the RE _not_ to match.

If you don't wish to use Perl (where '\s' means whitespace (spaces, tabs), then you probably want:

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

...where the character class in square brackets consists of a space _and_ a TAB character. Some 'sed' variants will allow:

\t

...to represent a TAB and you could be clearer by writing:

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

HP-UX doesn't understand this later notation.

Regards!

...JRF...
Torsten.
Acclaimed Contributor

Re: Getting rid of blank line

I assume the output is coming from a script - why not modify the script and remove the part that prints the new line?

Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
Bill Hassell
Honored Contributor

Re: Getting rid of blank line

The absolutely simplest way to remove a blank line:

awk NF /your/file
or
awk NF /your/oldfile > /your/newfile

It will remove any line that is null or contains nothing bu blanks or tabs. But as mentioned, you might want to fix the code that is outputting the extra line.


Bill Hassell, sysadmin
Dennis Handly
Acclaimed Contributor

Re: Getting rid of blank line

>JRF: Unfortunately, using the regular expresssion (RE): /^$/ ...

You can simply use a character class:
grep -v '^[[:space:]]*$'
http://docs.hp.com/en/B2355-60130/regexp.5.html#d0e1178990
James R. Ferguson
Acclaimed Contributor

Re: Getting rid of blank line

Hi:

Where I used the term "character class" for the bracketed space+TAB character, I should properly have said "bracket expression".

Regards!

...JRF...