Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-25-2007, 03:32 PM
|
#1
|
Member
Registered: Oct 2007
Posts: 40
Rep:
|
removing blank lines in a text file
how do I delete all blank lines in a text file? thanks 
|
|
|
10-25-2007, 03:36 PM
|
#2
|
Member
Registered: Jan 2007
Location: Portland, OR
Distribution: Ubuntu Jaunty / Fedora
Posts: 39
Rep:
|
There are a thousand ways to do that. If you want to do it from the ci:
Code:
cat file-blanklines-2-be-deleted | sed -e '/^$/d' | tee newfile-no-blanklines
Or, if you prefer vi/vim:
Code:
:g/^[ t]*$/d --> WHERE t=tab
|
|
|
10-25-2007, 03:37 PM
|
#3
|
ReliaFree Maintainer
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
|
|
|
|
10-25-2007, 03:38 PM
|
#4
|
Member
Registered: Apr 2004
Location: Scotland
Distribution: Suse, OpenWRT
Posts: 299
Rep:
|
Used this in the past to strip blank lines and comments
Code:
sed '/ *#/d; /^ *$/d' test.txt2
|
|
|
10-25-2007, 03:39 PM
|
#5
|
Member
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208
Rep:
|
Why couldn't you just use: sed /^$/d < oldfile > newfile
|
|
|
10-25-2007, 03:46 PM
|
#6
|
Member
Registered: Oct 2007
Distribution: rhel, fedora, gentoo, ubuntu, freebsd
Posts: 104
Rep:
|
I prefer grep:
#print only lines that contain non-whitespace characters
grep '[^[:space:]]' oldfile > newfile
|
|
|
10-25-2007, 06:39 PM
|
#7
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by bjagee
There are a thousand ways to do that. If you want to do it from the ci:
Code:
cat file-blanklines-2-be-deleted | sed -e '/^$/d' | tee newfile-no-blanklines
|
you can skin the cat
Code:
sed -e '/^$/d' file-blanklines-2-be-deleted
and if your sed supports -i option
Code:
sed -ie '/^$/d' file-blanklines-2-be-deleted
|
|
|
10-25-2007, 06:42 PM
|
#8
|
Senior Member
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
|
or vi the file and in command mode do this:
|
|
|
10-26-2007, 12:46 PM
|
#9
|
Member
Registered: Jan 2007
Location: Portland, OR
Distribution: Ubuntu Jaunty / Fedora
Posts: 39
Rep:
|
Quote:
Originally Posted by custangro
or vi the file and in command mode do this:
|
That will only eliminate spaces, and it will do so in sentences too. You would still have blank lines and tabsbutyourwordswouldallruntogether.
|
|
|
10-26-2007, 01:28 PM
|
#10
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
If you want to modify the original file (instead of creating a second, modified one), you can use the -i option. This version also deletes lines with spaces and tabs on... maybe you want to keep them, I don't know:
Code:
sed -i '/^[ \t]*$/d' the_file_to_modify
|
|
|
10-26-2007, 02:52 PM
|
#11
|
Senior Member
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
|
Quote:
Originally Posted by bjagee
That will only eliminate spaces, and it will do so in sentences too. You would still have blank lines and tabsbutyourwordswouldallruntogether.
|
Sorry, I miss understood the question, the correct syntax is this (in vi command mode):
-custangro
|
|
|
10-29-2007, 12:24 AM
|
#12
|
Member
Registered: Jan 2007
Location: Portland, OR
Distribution: Ubuntu Jaunty / Fedora
Posts: 39
Rep:
|
That's the ticket. You could also use \s in that to get lines that have non-printing chars:
|
|
|
All times are GMT -5. The time now is 10:02 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|