LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 10-25-2007, 04:32 PM   #1
christianunix
Member
 
Registered: Oct 2007
Posts: 40

Rep: Reputation: 15
removing blank lines in a text file


how do I delete all blank lines in a text file? thanks
 
Old 10-25-2007, 04:36 PM   #2
bjagee
Member
 
Registered: Jan 2007
Location: Portland, OR
Distribution: Ubuntu Jaunty / Fedora
Posts: 39

Rep: Reputation: 15
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
 
Old 10-25-2007, 04:37 PM   #3
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,750
Blog Entries: 1

Rep: Reputation: 238Reputation: 238Reputation: 238
Look at table C-2 --> http://tldp.org/LDP/abs/html/abs-guide.html#AEN20423
 
Old 10-25-2007, 04:38 PM   #4
The_JinJ
Member
 
Registered: Apr 2004
Location: Scotland
Distribution: Suse, OpenWRT
Posts: 299

Rep: Reputation: 30
Used this in the past to strip blank lines and comments

Code:
sed '/ *#/d; /^ *$/d' test.txt2
 
Old 10-25-2007, 04:39 PM   #5
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Rep: Reputation: 35
Why couldn't you just use: sed /^$/d < oldfile > newfile
 
Old 10-25-2007, 04:46 PM   #6
complich8
Member
 
Registered: Oct 2007
Distribution: rhel, fedora, gentoo, ubuntu, freebsd
Posts: 104

Rep: Reputation: 15
I prefer grep:
#print only lines that contain non-whitespace characters
grep '[^[:space:]]' oldfile > newfile
 
Old 10-25-2007, 07:39 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by bjagee View Post
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
 
Old 10-25-2007, 07:42 PM   #8
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , Solaris 10, RHEL
Posts: 1,935
Blog Entries: 1

Rep: Reputation: 188Reputation: 188
or vi the file and in command mode do this:

Code:
%s/ //g
 
Old 10-26-2007, 01:46 PM   #9
bjagee
Member
 
Registered: Jan 2007
Location: Portland, OR
Distribution: Ubuntu Jaunty / Fedora
Posts: 39

Rep: Reputation: 15
Quote:
Originally Posted by custangro View Post
or vi the file and in command mode do this:

Code:
%s/ //g
That will only eliminate spaces, and it will do so in sentences too. You would still have blank lines and tabsbutyourwordswouldallruntogether.
 
Old 10-26-2007, 02:28 PM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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
 
Old 10-26-2007, 03:52 PM   #11
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , Solaris 10, RHEL
Posts: 1,935
Blog Entries: 1

Rep: Reputation: 188Reputation: 188
Quote:
Originally Posted by bjagee View Post
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):

Code:
g/^$/d
-custangro
 
Old 10-29-2007, 01:24 AM   #12
bjagee
Member
 
Registered: Jan 2007
Location: Portland, OR
Distribution: Ubuntu Jaunty / Fedora
Posts: 39

Rep: Reputation: 15
That's the ticket. You could also use \s in that to get lines that have non-printing chars:
Code:
: %s:\s:g
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing new lines from a file psandeepnair1985 Programming 5 03-25-2007 12:46 PM
Grab text lines in text file LULUSNATCH Programming 1 12-02-2005 11:55 AM
Removing lines from file Aylar Programming 2 04-22-2004 07:34 AM
How do i remove blank lines from a file? kakho Programming 1 04-15-2004 04:57 AM
Replace blank/almost blank lines in file Wynd Linux - General 3 01-27-2004 05:49 PM


All times are GMT -5. The time now is 12:34 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration