Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sed command

Status
Not open for further replies.

ledong

Technical User
Oct 3, 2000
9
US
Hi there,
What is the command line do you use to delete all blank line in a file with sed command.
I had tried for a week but could not success. I am new in this toy. Please help.
Thanks.

Dong [sig][/sig]
 
grep -v &quot;^$&quot; < inputfile > outputfile

This will remove blank empty lines but not if they contain white space.
[sig]<p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>Top man[/sig]
 
Thanks for your help but I need to know how to use the sed command to remove a blank line in a file.
Please help.

Dong
ledong@hotmail.com [sig][/sig]
 
Well I generaly replace spaces and tabs with a particular charecter then exluce them: -

To get all data lines: -

egrep &quot;[A-z]|[0-9]&quot; < inputfile > outputfile

[sig]<p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>Top man[/sig]
 
If all the lines of data contain alpha you could also try :-

sed '/[A-z]/!d' < infile > outfile

or for numeric

sed '/[0-9]/!d'

Note, [A-z] is for all upper and lower case alpha charecters. !d means delete lines not containing the string charecters. [sig]<p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>Top man[/sig]
 
If you have to use sed, it's not too different from the &quot;grep&quot; command suggested by Ged:
[tt]
sed -e's/^$//' input_file_name > new_file_name
[/tt]
This passes the file &quot;input_file_name&quot; into sed, which swaps all blank lines for nothing, and sends the output to &quot;new_file_name&quot;.
[sig]<p> Andy Bold<br><a href=mailto: > </a><br><a href= > </a><br>"I've probably made most of the mistakes already, so hopefully you won't have to..." Me, most days.[/sig]
 
Thanks for all of your help. [sig][/sig]
 
Maybe this can be helpful, from the the Unix Command Compendium,

sed '/--*$/{n;/^$/d;}' file_name

This example deletes blank lines that follow the first regular expression--which, for example purposes, consists of two or more hyphens. The n command tells sed to process the line following this regular expression. Next, the regular expression /^$/ specifies that only blank lines should be processed. Finally, the d command tells sed to delete the blank lines.

sed '{n;/^$/d;}' file_name > new_file_name

The above will remove all blank lines from your file. If you have to do this repeatedly to many files I would suggest you script this and use variables for file_name and new_file_name.>:):O> [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top