search for keyword in subsequent lines and delete the second line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search for keyword in subsequent lines and delete the second line
# 1  
Old 08-21-2009
search for keyword in subsequent lines and delete the second line

I have my data something like this

I need to search for the keyword yyyy in the susequent lines and if it is present, delete the second line with keyword.

In other words, if a keywords is found in two subsequent lines delete the second line.

input data:
Code:
aaaa bbbbb cccc dddd
xxxx yyyy xxxxx abc
xxxx yyyy xxxxx xyz
aaaa bbbbb cccc dddd
rrrrrrrrr ttttttt yyyy
xxxx yyyy xxxx
aaaa bbbbb cccc dddd

Code:
output expected:
aaaa bbbbb cccc dddd
xxxx yyyy xxxxx abc
aaaa bbbbb cccc dddd
rrrrrrrrr ttttttt yyyy
xxxx yyyy xxxx
aaaa bbbbb cccc dddd

# 2  
Old 08-21-2009
shouldn't the output be this?
Code:
aaaa bbbbb cccc dddd
xxxx yyyy xxxxx abc
aaaa bbbbb cccc dddd
rrrrrrrrr ttttttt yyyy
aaaa bbbbb cccc dddd

# 3  
Old 08-21-2009
Yes..You are correct!

output expected is

Code:
aaaa bbbbb cccc dddd
xxxx yyyy xxxxx abc
aaaa bbbbb cccc dddd
rrrrrrrrr ttttttt yyyy
aaaa bbbbb cccc dddd

# 4  
Old 08-21-2009
Should be something like this:

Code:
awk '/yyyy/ && NR!=n {n=NR+1}
/yyyy/ && n==NR {next}
{print}' file

# 5  
Old 08-21-2009
That worked Perfect!

thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a Keyword in file and replace another keyword or add at the end of line

Hi I want to implement something like this: if( keyword1 exists) then check if(keyword2 exists in the same line) then replace keyword 2 with New_Keyword else Add New_Keyword at the end of line end if eg: Check for Keyword JUNGLE and add/replace... (7 Replies)
Discussion started by: dashing201
7 Replies

2. UNIX for Advanced & Expert Users

Need command for grepping pattern lines with subsequent lines

Hi, I have a requirement like, I have a list of pattens in a file say pattern.txt, PHC111 PHC113 and in another file called master.lst i have entries like, PHC111 a b PHC112 a PHC113 b c PHC114 d e (5 Replies)
Discussion started by: rbalaj16
5 Replies

3. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

4. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

5. Shell Programming and Scripting

Using sed to delete a line having a particular keyword

Hi Geeks :b:, I need to delete a line from file that contains a particular keyword. I had read in some forum of unix.com that below code could be used sed "/$titlesearch/d" movielist >tmp mv tmp movielist But my file contains lines which contain slashes (/) FOr eg: /etc/movie/title/... (5 Replies)
Discussion started by: ajincoep
5 Replies

6. Shell Programming and Scripting

How to search for keywords in subsequent lines

Hi all, I am looking for a coomand to search for the keywords in susequenct lines. Keyword1 in a line and Keyword2 in the very next line. Once i found the combination ineed to print the lines with patterns and the line above and one below. I am giving an example here: Keywords are :ERROR and... (12 Replies)
Discussion started by: rdhanek
12 Replies

7. Shell Programming and Scripting

Delete the lines before the first instance of the keyword

I have my data something like this. I want to delete all the lines before the frist instance of the key word 'ravi kumar' aaa bbbbbb cccccc ddddd eeeee 1234 ravi kumar aaaaaa vvvvvvv 5678 ravi kumar rrrrrrr mmmmmmm I want the output as follows. 1234 ravi kumar aaaaaa... (8 Replies)
Discussion started by: rdhanek
8 Replies

8. Shell Programming and Scripting

Delete the lines after the last instance of the keyword

I have my input sometyhing like this aaa bbbbbb cccccc ddddd eeeee 1234 ravi kumar aaaaaa vvvvvvv 5678 ravi kumar rrrrrrr mmmmmmm I want the output as follows. aaa bbbbbb cccccc ddddd eeeee 1234 ravi kumar aaaaaa vvvvvvv 5678 ravi kumar (2 Replies)
Discussion started by: rdhanek
2 Replies

9. Shell Programming and Scripting

replace only 1st word of a line if it comes in the subsequent lines at same postion.

I have a file like this.. Maharastra Mumbai worli Maharastra Mumbai navy maharatra Pune Maharastra Nagpur Karnataka Bangalore Karnataka Mysore Karnataka Mangalore Punjab Amritsar punjab Jalandar my expected outcome should be like this Maharastra Mumbai worli ---------- ... (9 Replies)
Discussion started by: geeko
9 Replies

10. Shell Programming and Scripting

search 2 lines and delete above line

Hi, I've been searching in this forum for the last 4 hours trying to do one thing: search 2 lines and delete the above line. So far I have not be able to find something similar in this forum, so I need help. This is what I'm trying to do. For example, I have a file called file1: file1 word1... (4 Replies)
Discussion started by: shamushamu
4 Replies
Login or Register to Ask a Question