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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove empty lines(tabs and spaces)

Status
Not open for further replies.

begi

IS-IT--Management
Jun 27, 2002
1
FR
Hi,

I have a file which contains many empty
lines some with tabs and others just with
spaces or a combination of both. How can
I remove these lines keeping the rest of
the file intact.

Any help appreciated.

/A Beginner.
 
The answer is to use sed
To remove empty lines :
cat filename |sed '/^$/d' > newfile

then To remove lines with just a space :
cat newfile | sed 's/^ //' > filename

or to remove tabs
sed 's/'"$(printf '\009')"'$// File > Filenew

or sed 's/'`echo "\015"`'//g' file > filenew

HTH ;-)
Dickie Bird
db@dickiebird.freeserve.co.uk
 
Oops last line ( sed 's/'`echo "\015"`'//g' file>filenew ) should read "\009" because 015 is ^0 ( these examples were cribbed from existing scripts of mine)

Dickie Bird
db@dickiebird.freeserve.co.uk
 
Code:
sed '/^[  ]*$/d' file > newfile
where the characters in the square brackets are a space followed by a tab, will remove lines which are empty or contain only spaces or tabs.

Hope this helps. CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top