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?
|
grep -v "^$" filename > newfilename
|
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 |
hmm, why not use sed to do it in one command :?
# delete lines matching pattern Code:
cat file.txt| sed '/pattern/d' |
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.
|
@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. |