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

Removing empty lines in a text file - but not the other line breakes

Status
Not open for further replies.

Larshg

Programmer
Mar 1, 2001
187
DK
Hi

I would like to remove empty lines in a text fil, but not the other line breakes

ex. - a text file like this

./cat text.txt
test1

test3
test5

I want the output to be
test1
test3
test5

I've tryede something like this
tr '\012' ' ' < ./text.txt

but this removes all the line breakes and put everythin into one line.


/Larshg
 
Try something like this:
sed -e '/^$/d' ./text.txt

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
And the awk way:
awk 'NF' ./text.txt

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Larshg, as you have posted a new question in this forum, I assume you have read the replies in your previous post.
It would be fair (for the members) to close your threads by saying how your problem was solved or not.
Keep in mind that you aren't in a help desk site here.
 
The two commands 'sed' and 'awk' doesn't do exactly the same thing

[tt]sed -e '/^$/d'[/tt] removes empty lines

[tt]awk 'NF'[/tt] removes empty lines and lines with only spaces or tabs, like :
[tt]sed '/^[[:space:]]*$/d'[/tt]

The sed equiv is :
[tt]awk -F '' 'NF'[/tt]




Jean Pierre.
 
Thanks that did it for me.

I've usede the "awk way" - becourse I only wanted lines that contain some text

/Larshg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top