LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 02-20-2006, 09:12 PM   #1
Serena
Member
 
Registered: Apr 2002
Distribution: Debian 11.0
Posts: 97

Rep: Reputation: 16
Question getting rid of extra blanks lines


I used cat filename1.txt | grep -v foo > filename2.txt to remove all lines with certain content. But now I have a lot of blank lines where those lines were. So now, how can I use grep or sed to get rid of instances of 2+ blank lines while leaving single blank lines alone?
 
Old 02-20-2006, 09:18 PM   #2
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
grep -v "^$" filename > newfilename

Last edited by Simon Bridge; 02-20-2006 at 09:20 PM.
 
Old 02-20-2006, 11:14 PM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Here's an example from the mother load of sed examples:
http://sed.sourceforge.net/sed1line.txt
Code:
sed '$!N; /^\(.*\)\n\1$/!P; D' file.txt
 
Old 02-21-2006, 02:00 AM   #4
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
hmm, why not use sed to do it in one command :?
# delete lines matching pattern
Code:
cat file.txt| sed '/pattern/d'
 
Old 02-27-2006, 12:35 AM   #5
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
muha: well - sed already can receive an input filename so it's unclear what you need the pipe for ... anyway, that's what homey has done isn't it? The main trouble was coming up with the "pattern" for a newline to type into the command - something you havn't provided. Take a careful look at the above two examples.
 
Old 02-27-2006, 02:09 AM   #6
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
@the pipe: i agree, i could be just like: sed '/foo/d' file.txt
I thought that the pipe was a faster way, timewise.

But what i meant is, instead of doing:
cat filename1.txt | grep -v foo > filename2.txt
followed by: sed '$!N; /^\(.*\)\n\1$/!P; D' file.txt

Get rid of lines (with certain content) in one step with:
sed '/foo/d' file.txt
(which is what i think the OP was actually trying to do in the beginning ..)

Last edited by muha; 04-10-2006 at 02:21 AM.
 
  


Reply



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
screensaver blanks out TomalakBORG SUSE / openSUSE 7 12-17-2005 12:11 AM
Extra lines pforget Slackware 8 11-25-2005 10:18 AM
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 02:03 PM
found two lines extra in my fstab amer_58 Linux - Newbie 3 08-20-2005 07:56 AM
Monitor blanks out vincebs Mandriva 5 01-16-2004 04:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:44 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration