Visit Our UNIX and Linux User Community


How to search for keywords in subsequent lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to search for keywords in subsequent lines
# 8  
Old 08-10-2009
I get the desired output with my code:

Code:
$ cat file
aaaaaaaa bbbbbbbbb vvvvvvvv
(:ERROR)  ccccccccc dddddddddddd eeeeeeeee kkkkk
Found Path: rrrrrr bbbb
cccccccccccc vvvvvvvvvvv kkkk rrrrrrrrr
aaaaaaaa bbbbbbbbb vvvvvvvv fffffffff 
(:ERROR)  aaaaaaaaaaa bnbbbbbbbbbbb ccccccc
ggggggggg hhhhhhhhhhhhhh rrrrrrrr
xxxxxxxxxx yyyyyyyyyyyyy zzzzzzzzzzzz
(:ERROR)  rrrrrrrrrrrr ssssssss tttttttt
Found Path: ggghhhhh sshshshshs
pppppppppp qqqqqqqq rrrrrrrrrrr
$ awk '/(:ERROR)/{err=$0;getline;if($0 ~ /^Found Path/){print prev RS err RS $0;getline; print}}{prev=$0}' file
aaaaaaaa bbbbbbbbb vvvvvvvv
(:ERROR)  ccccccccc dddddddddddd eeeeeeeee kkkkk
Found Path: rrrrrr bbbb
cccccccccccc vvvvvvvvvvv kkkk rrrrrrrrr
xxxxxxxxxx yyyyyyyyyyyyy zzzzzzzzzzzz
(:ERROR)  rrrrrrrrrrrr ssssssss tttttttt
Found Path: ggghhhhh sshshshshs
pppppppppp qqqqqqqq rrrrrrrrrrr

Am I missing something?
# 9  
Old 08-10-2009
Sorry, That was my mistake. Your code works well.
Can you please give me the code for removing the above code's output from the file? So that i can work on the remaining data

expected output is

Code:
aaaaaaaa bbbbbbbbb vvvvvvvv fffffffff 
(:ERROR)  aaaaaaaaaaa bnbbbbbbbbbbb ccccccc
ggggggggg hhhhhhhhhhhhhh rrrrrrrr

# 10  
Old 08-10-2009
Quote:
Originally Posted by rdhanek
Sorry, That was my mistake. Your code works well.
Can you please give me the code for removing the above code's output from the file? So that i can work on the remaining data

expected output is

Code:
aaaaaaaa bbbbbbbbb vvvvvvvv fffffffff 
(:ERROR)  aaaaaaaaaaa bnbbbbbbbbbbb ccccccc
ggggggggg hhhhhhhhhhhhhh rrrrrrrr

The second output is print to the file new_output.

Code:
awk '
/(:ERROR)/{
  err=$0;getline
  if($0 ~ /^Found Path/){
     print prev RS err RS $0;getline; print
     next
  } else {
    print prev RS err RS $0 > "new_output"
  }
}
{prev=$0}
' file > newfile
mv newfile > file

Regards
# 11  
Old 08-11-2009
Thanks a lot. It worked perfectly.

---------- Post updated 08-11-09 at 04:18 AM ---------- Previous update was 08-10-09 at 10:13 AM ----------

Hello Franklin52,

Need an update in the code that you have provided.. Currently it is only considering the lines with keyword ERROR to start the search. In fact there are some lines in teh data without keywords ERROR and Found Path. I need those lines to be remained in the main file once we separate the lines with Pattern. Could you please re-look into this?

input data

Code:
aaaaaaaa bbbbbbbbb vvvvvvvv
(:ERROR)  ccccccccc dddddddddddd eeeeeeeee kkkkk
Found Path: rrrrrr bbbb
cccccccccccc vvvvvvvvvvv kkkk rrrrrrrrr
aaaaaaaa bbbbbbbbb vvvvvvvv fffffffff
(:ERROR)  aaaaaaaaaaa bnbbbbbbbbbbb ccccccc
ggggggggg hhhhhhhhhhhhhh rrrrrrrr
xxxxxxxxxx yyyyyyyyyyyyy zzzzzzzzzzzz
(:ERROR)  rrrrrrrrrrrr ssssssss tttttttt
Found Path: ggghhhhh sshshshshs
pppppppppp qqqqqqqq rrrrrrrrrrr
rrrrrrrrrrrr ssssssss tttttttt
Found Path: ggghhhhh sshshshshs
pppppppppp qqqqqqqq rrrrrrrrrrr
rrrrrrrrrrrr ssssssss tttttttt
Found Path: ggghhhhh sshshshshs
pppppppppp qqqqqqqq rrrrrrrrrrr

output1
Code:
aaaaaaaa bbbbbbbbb vvvvvvvv
(:ERROR)  ccccccccc dddddddddddd eeeeeeeee kkkkk
Found Path: rrrrrr bbbb
cccccccccccc vvvvvvvvvvv kkkk rrrrrrrrr
xxxxxxxxxx yyyyyyyyyyyyy zzzzzzzzzzzz
(:ERROR)  rrrrrrrrrrrr ssssssss tttttttt
Found Path: ggghhhhh sshshshshs
pppppppppp qqqqqqqq rrrrrrrrrrr

Out put2(remaing file after the data seperated to output1)
Code:
aaaaaaaa bbbbbbbbb vvvvvvvv fffffffff
(:ERROR)  aaaaaaaaaaa bnbbbbbbbbbbb ccccccc
ggggggggg hhhhhhhhhhhhhh rrrrrrrr
rrrrrrrrrrrr ssssssss tttttttt
Found Path: ggghhhhh sshshshshs
pppppppppp qqqqqqqq rrrrrrrrrrr
rrrrrrrrrrrr ssssssss tttttttt
Found Path: ggghhhhh sshshshshs
pppppppppp qqqqqqqq rrrrrrrrrrr

# 12  
Old 08-11-2009
You can use the first command to produce the first file and grep to get the other lines:

Code:
grep -v -f output1 inputdata

# 13  
Old 08-11-2009
I am getting the below error. Not sure if it is supported under SunOS box. Can you suggest me an alternative?

Code:
grep: illegal option -- f


Previous Thread | Next Thread
Test Your Knowledge in Computers #942
Difficulty: Medium
The underlying Data Link Layer never poses limits to the maximum frame size.
True or False?

9 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

3. Shell Programming and Scripting

awk - use fields from subsequent lines

I've run into a problem getting exactly what I want out of awk - some folks may recognize this as an output from Amazon's ec2-describe-instances: Given the following: INSTANCE i-4960f321 BLOCKDEVICE Line2Var2 TAG instance i-4960f321 Name web1 TAG instance i-4960f321... (2 Replies)
Discussion started by: colinjohnson
2 Replies

4. Shell Programming and Scripting

Search a file with keywords

Hi All I have a file of format asdf asf first sec endi asdk rt 123 ferf dfg ijglkp (7 Replies)
Discussion started by: mailabdulbari
7 Replies

5. Shell Programming and Scripting

Find pattern, and then last field from subsequent lines

I've got a log file, of the format Name: network1 Dropped packets: 15618 Dropped packets for IPv6: 27 Dropped packets: 74 Dropped packets for IPv6: 0 Failed RADIUS Authentication procedures: 0 Failed RADIUS Accounting procedures: 0 Name: network2 Dropped packets: 1117 ... (18 Replies)
Discussion started by: Yorkie99
18 Replies

6. Shell Programming and Scripting

Grep and fetch subsequent lines also

Hi, I need to grep a pattern and fetch subsequent lines till end of the data-set. E.g., i have a file like: AA 1111 23 34 BB 45 56 78 CC 22 44 AA 2222 78 34 56 BB 22 56 67 68 23 CC 56 78 DD 33 55 77 AA 3333 46 BB 58 79 In above file i have 3-data sets where each set starts with... (6 Replies)
Discussion started by: prvnrk
6 Replies

7. Shell Programming and Scripting

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: aaaa bbbbb cccc dddd xxxx... (4 Replies)
Discussion started by: rdhanek
4 Replies

8. Shell Programming and Scripting

How to extract a substring and append to subsequent lines

Hi all,I am really new to Shell Scripting.I have the following doubt. Let us assume the one sample file which contains the below data HEADERCARMENTRACIE1555090414 PERIOD0905090501090531 DETAIL0645693037023073836 GROUNDAV 090501 01 GROUNDAV 090502 01 TRIP 0091282542 0905084101... (5 Replies)
Discussion started by: jaligamasriniva
5 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

Featured Tech Videos