LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to delete two adjecent lines in a text file (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-delete-two-adjecent-lines-in-a-text-file-612421/)

wangxinmco 01-09-2008 03:34 PM

how to delete two adjecent lines in a text file
 
hello, experts,
I need to delete the lines containing letter "a" and the next line together from a tect file.
I found the two lines with
fgrep a -A 1 filename
But I do not know how to delete them.

Please help me,

Thank you in advance,

ghostdog74 01-09-2008 07:39 PM

Code:

awk '/a/{getline;next}1' file

osor 01-10-2008 01:35 PM

Quote:

Originally Posted by ghostdog74 (Post 3017291)
Code:

awk '/a/{getline;next}1' file

The problem with this sort of approach is that it removes two lines without applying the pattern match to next itself. This might cause trouble when you have an even number of consecutive lines containing the character ‘a’ in your file.

For example, if file is the following:
Code:

a
ba
c
d

Your filter will result in the following output:
Code:

$ awk '/a/{getline;next}1' file
c
d

But the desired output would be:
Code:

d
It is unclear (to me anyway) if the OP is affected by this problem.

bigrigdriver 01-10-2008 02:23 PM

sed -e '/a/,2D' <filename>
will delete all lines with 'a' in them, but the first instance will also delete the following line.


All times are GMT -5. The time now is 08:09 AM.