Computing.Net > Forums > Unix > blank lines and sed

blank lines and sed

Reply to Message Icon

Original Message
Name: alrinno
Date: July 15, 2008 at 23:15:31 Pacific
Subject: blank lines and sed
OS: UNIX
CPU/Ram: N/A
Model/Manufacturer: Any
Comment:

----------------------

hello,

i have tried to remove blank lines from a file using korn shell script file.. it doesn't seem to work!

i tried sed '/^\s*$/d' infile > outfile but that didn't work
i tried sed 's/ *$//;/^$/d' infile > outfile and that didn't work
i tried sed '/^s./d' infile > outfile and that didn't work

what can i do? this is a huge file and some blank lines might be spaces? or tabs? how can i know?

thank you


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: July 16, 2008 at 09:59:38 Pacific
Reply: (edit)

On my Solaris 9 box, this works:

#!/bin/ksh

sed '/^[ ]*$/d' tfile

Be advised that with the above one-liner between the brackets:

[ ]

is a space bar and a tab key. Some versions of sed allow using \t as a tab.


Report Offensive Follow Up For Removal

Response Number 2
Name: lankrypt0
Date: July 16, 2008 at 12:42:19 Pacific
Reply: (edit)

if it does not have to be sed, you could use tr:
tr -s "\n"


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: July 16, 2008 at 13:04:19 Pacific
Reply: (edit)

The tr solution handles only newlines; it does not handle lines that are all spaces or tabs.


Report Offensive Follow Up For Removal

Response Number 4
Name: James Boothe
Date: July 16, 2008 at 15:19:08 Pacific
Reply: (edit)

awk can check for Number of Fields greater than zero.

awk 'NF>0' infile > outfile


Report Offensive Follow Up For Removal

Response Number 5
Name: alrinno
Date: July 17, 2008 at 10:40:47 Pacific
Reply: (edit)

Thank you all for your help!!!


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: blank lines and sed

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge