Paul
Paul

Reputation: 1

Delete only the line following the regex while retaining the line with the regex

Ok, I'm completely stumped...

Scenario: An .rtf file with 100 lines of text when viewed with Word. The word "Linux" on one line with a blank line below it. I need to retain the line with "Linux" and remove the blank line below it. However, the line isn't truly blank when looking at it with vi, so none of the posts dealing with removing a blank line don't work. Also, I can find plenty of posts to remove the line with the regex AND the next line, but I need to keep the regex line. There is only one line in the file with the word "Linux".

I know sed can do this, but haven't been able to figure it out.

Upvotes: 0

Views: 111

Answers (2)

Scrutinizer
Scrutinizer

Reputation: 9936

Maybe this? I had a look at an RTF file, it looked as if there were carriage returns separating the lines. So I used those as record separators and removed one line and and was able to read the resulting file and the line was gone. I have no idea if it would work in other situations. You'd have to test thoroughly if it works. I used awk, since rtf files are not "valid" text files in Unix/Linux, and I think probably sed cannot do it..

awk 'n-->0{next} /Linux/{n=1}1' RS='\r' ORS='\r' infile.rtf > outfile.rtf

If it works it should discard the line below the line with the word "Linux".

Upvotes: 0

Beta
Beta

Reputation: 99172

sed '/Linux/{n;d;}'

(...And an Answer must contain at least 30 characters.)

Upvotes: 1

Related Questions