LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   getting rid of extra blanks lines (https://www.linuxquestions.org/questions/linux-newbie-8/getting-rid-of-extra-blanks-lines-417787/)

Serena 02-20-2006 09:12 PM

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?

Simon Bridge 02-20-2006 09:18 PM

grep -v "^$" filename > newfilename

homey 02-20-2006 11:14 PM

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

muha 02-21-2006 02:00 AM

hmm, why not use sed to do it in one command :?
# delete lines matching pattern
Code:

cat file.txt| sed '/pattern/d'

Simon Bridge 02-27-2006 12:35 AM

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.

muha 02-27-2006 02:09 AM

@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 ..)


All times are GMT -5. The time now is 06:22 PM.