Grep and fetch subsequent lines also


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep and fetch subsequent lines also
# 1  
Old 11-09-2009
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 "AA".

if i grep for "2222" then i should get the below output (4 lines):

AA 2222 78 34 56
BB 22 56 67 68 23
CC 56 78
DD 33 55 77.

Likewise if i grep for "1111" then output should be as below:
AA 1111 23 34
BB 45 56 78
CC 22 44

Pl advise.
TIA
prvn
# 2  
Old 11-09-2009
grep is not the right tool to use.
Code:
$ awk 'BEGIN{ toget="2222" ;}$1=="AA"{g=0}$1=="AA" && $2==toget {g=1}g' file

# 3  
Old 11-09-2009
Thank you for the reply.

But i'm getting blank output.

As the field separator is "|" (not blank; sorry, i didn't mention this in my original post) i tried below but getting blank output. As i am using Solaris, i used "gawk".

Code:
#gawk -F"|" 'BEGIN{ toget="25829649" ;}$1=="JKL"{g=0}$1=="JKL" && $2==toget {g=1}g' rpk.in
#

Please note that whatever i am grepping (ex. 2222) need not be in the first line (line with AA).

Thanks again,
Prvn
# 4  
Old 11-09-2009
Something like this perhaps?
Code:
$> cat test2
awk 'function printm() {if (m==1){for (r in A){print A[r]}};delete A;m=0} $1=="AA"{ printm()} {A[r++]=$0} /'$1'/{m=1} END{printm()}' rpk.in

Note that the part in red refers to the $1 of the surrounding shell script.

Code:
$> ./test2 1111
AA 1111 23 34
BB 45 56 78
CC 22 44
$>  ./test2 3333
AA 3333 46
BB 58 79
$>  ./test2 58
AA 3333 46
BB 58 79

If your file is '|' separated then use awk -F '|'
# 5  
Old 11-09-2009
perl a.pl:

Code:
use Getopt::Std;
getopt('k');
local $/="--";
open FH,"sed 's/^AA /--AA /' yourfile.txt|";
while(<FH>){
        s/--//;
        print $_ if /\b$opt_k\b/;
}

Usage:
Code:
perl a.pl -k 1111
perl a.pl -k 2222

# 6  
Old 11-10-2009
Thank you Scrutinizer!! It worked like a charm.

summer_cherry - Thank you, i will check your solution as well and let you know.
# 7  
Old 11-10-2009
Derivated from ghostdog74 solution :
Code:
awk -v value=$1 '
/^AA/ { display = ($2 == value) }
display
' pv.dat

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to fetch the value from a xml using sed, GREP?

I have a simple xml file,need the output with the <value> tag and <result> tag text.xml <test-method status="FAIL" duration="45"> <value> Id=C18 </value> <result> wrong paramter </result> </test-method> <test-method status="FAIL" duration="45"> <value> Id=C19 </value> <result> Data... (5 Replies)
Discussion started by: DevAakash
5 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

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

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

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

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

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

10. Shell Programming and Scripting

Copy subsequent contents of a file from first occurance of grep

There is a file which logs all errors and alerts of the database called alert log. I have a requirement as follows: 1. Check the current date and search for the first occurance of the current date in the alert log. 2. As soon as the first occurance is found, copy the subsequent contents... (5 Replies)
Discussion started by: sunpraveen
5 Replies
Login or Register to Ask a Question