How to Grep than scan line below grep pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Grep than scan line below grep pattern
# 1  
Old 10-03-2013
How to Grep than scan line below grep pattern

Hello Colleagues,

I have a file that looks like below.

Code:
6-12731913-12731913
9230760143480
410018547148230
20131002193434+0500
20131002193434+0500
;20131002T161031000-10.50.241.21-21912131-1419034760, ver: 0
20131009
92220056296730
CC0P
abc
Core_Context_R1A
SMS
6-12726796-12726796
9230349094420
410016350040971
20131002193434+0500
20131002193434+0500
;20131002T161031000-10.50.241.21-21912131-1419034760, ver: 0
20131008
92775313215350
92DDD060
CC0P
abc
Core_Context_R1A
Voice
6-12725266-12725266
9230172005830
410018898077989
20131002193434+0500
20131002193434+0500
920131002T164612000-10.50.241.21-17667023-92612900, ver: 0
20131009
0P(h3
92780065437090
0P(h3
CC0P
abc
Core_Context_R1A
GPRS
6-12726796-12726796
9230349094420
410016350040971
20131002193439+0500
20131002193439+0500
;20131002T161031000-10.50.241.21-21912131-1419034760, ver: 0
20131008
92775313215350
92DDD060
CC0P
abc
Core_Context_R1A
Voice

Now I want to grep 20131002 and if it finds pattern "20131002" it should scan lines below "20131002" and if it finds the word "Voice" that comes after grepping "20131002, than script print out all such occurrences in the file. Script Output would look like below.

20131002193434+0500 Voice
20131002193439+0500 Voice

Can any one please help me, how to implement this using shell script?

Regards,
Umar

Last edited by umarsatti; 10-03-2013 at 03:42 PM..
# 2  
Old 10-03-2013
Just thinking...

What about:
Code:
cat -n myfile | grep "20131002" | cut -f1

tells you the line #
and so does
Code:
cat -n myfile | grep "Voice" | cut -f1

so, you could assign each of these to a variable, and then compare the two variables?
# 3  
Old 10-03-2013
If the very first line of the file has 20131002, and the very last line of the file has Voice, with no other hits inbetween -- is that a valid match? If not, why not?

In short, what tells it where to stop?
# 4  
Old 10-03-2013
No first line is not "20131002" and last line is not "Voice", script first grep 20131002 and than scan lines below as soon as it come across first "Voice" it prints and search for other such patterns by grepping "20131002" and than lines below it, to search "Voice". And than prints all such patterns found.
# 5  
Old 10-03-2013
So, there could be more than one pair of data?
20131002 and Voice
# 6  
Old 10-03-2013
Yes, sorry for the confusion, let me put it in more simple way.

Example:

Code:
20130210132030
A
B
C
Banana
20130210142320
D
E
F
Mango
20130210154634
G
H
I
Apple
20130210163415
J
K
L
Mango
20130210171829
M
N
O
Apple

So my script first grep pattern "20130210" once found, it should scan line below and scan for Apple, if Apple is found it should print. Output would look like below
"20130210154634 Apple"
"20130210171829 Apple"

Apple is printed to its corresponding "20130210" only, which is unique.

But if grep pattern "20130210" once found and it scans lines below and finds "Banana" or "Mango" it should exit and search for other grep pattern "20130210" and scan lines below it, as soon as it finds the first "Apple" it should exit and find other such patterns.

I hope its clear now?

BR/Umar
# 7  
Old 10-03-2013
If your awk supports multi-character record separators, you could do something like:
Code:
$ awk '/Apple/ {printf RS $1 OFS "Apple\n"}' RS="20130210" fruit
20130210154634 Apple
20130210171829 Apple


Last edited by CarloM; 10-03-2013 at 06:39 PM.. Reason: Fixed output format
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep file starting from pattern matching line

I have a file with a list of references towards the end and want to apply a grep for some string. text .... @unnumbered References @sp 1 @paragraphindent 0 2017. @strong{Chalenski, D.A.}; Wang, K.; Tatanova, Maria; Lopez, Jorge L.; Hatchell, P.; Dutta, P.; @strong{Small airgun... (1 Reply)
Discussion started by: kristinu
1 Replies

2. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

3. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. Shell Programming and Scripting

grep based on pattern in a line and print the column before that

$ cat file.log Message Number = : Sending message 10:50:16^|^reqhdr.dummyid^=^02^|^reqhdr.timezone^=^GMT+05:30^|^DUMMYREQUEST^=^BH||||||||||||||||||$BD|OL|C|V||DummyAcctNo|02||24/12/2011|ST_DDM|DDM||||||||reqUUID110612105016$BT||||||||||||||||||$] Length I have the above line in the... (4 Replies)
Discussion started by: kalidass
4 Replies

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

6. Shell Programming and Scripting

grep a pattern with line numbers.

I have a txt file with more than 10000 lines. There is a unique pattern which is scattered into the file. it starts with @9 and it has 15 characters. i need to grep them and display along with line numbers. Eg: File - Test1 test message.... .... .. .. @9qwerty89 ...test message... (8 Replies)
Discussion started by: abinash
8 Replies

7. Shell Programming and Scripting

Grep multiple line pattern and output the lines

Hi I have the following Input -- -- TABLE: BUSINESS_UNIT -- ALTER TABLE RATINGS.BUSINESS_UNIT ADD CONSTRAINT FK1_BUSINESS_UNIT FOREIGN KEY (PEOPLESOFT_CHART_FIELD_VALUE_ID) REFERENCES RATINGS.PEOPLESOFT_CHART_FIELD_VALUE(PEOPLESOFT_CHART_FIELD_VALUE_ID) ; ALTER TABLE... (1 Reply)
Discussion started by: pukars4u
1 Replies

8. Solaris

Multiple pattern on same line using grep

Hi, I would like to search multiple patterns on same line, i.e. all patterns must present on same line. Please suggest. Thanx (2 Replies)
Discussion started by: sanjay1979
2 Replies

9. UNIX for Dummies Questions & Answers

grep line pattern search

Hello everyone, I have been trying to get a list of all files containing a line of this type: };#followed by anything with any spaces (0 or more or 0 or more tabs) before the } and between each of the characters. I have been trying this : grep '*}*;*#*' *.c but I have not been fully... (1 Reply)
Discussion started by: gio001
1 Replies

10. Solaris

How do i grep a pattern from part of LINE

Hi how do i grep only a part of the line from a file from all the lines. file: asdfgh 543212376 nag lkjh abhilash 543757858 How do i grep and print only 543212376 and 543757858 Can i grep something like 543* and print only that. (3 Replies)
Discussion started by: 123nagendrabhi
3 Replies
Login or Register to Ask a Question