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
# 8  
Old 10-03-2013
Code:
while read line || [ -n "$line" ]; do
	case $line in
		\;*)		continue;;
		*20131002*)	stored=$line;;
		*Voice*)	echo "$stored $line";;
	esac
done < file


Last edited by jethrow; 10-04-2013 at 12:01 AM..
# 9  
Old 10-04-2013
Another awk version
Code:
awk '/20130210/ {p=$0} /Apple/ {print p,$0}' file
20130210154634 Apple
20130210171829 Apple

# 10  
Old 10-04-2013
One more approach
Code:
$ cat t.txt
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

Code:
awk '{p=$0;getline;getline;getline;getline;if($0~/Apple/)printf p"\t"$0"\n";p=$0}' t.txt
20130210154634    Apple
20130210171829    Apple


Last edited by Akshay Hegde; 10-04-2013 at 08:48 AM..
# 11  
Old 10-04-2013
Hi,
Just for fun (work under linux) Smilie
Code:
$ grep '20130210\|Apple' file | uniq -w8 | paste -d' ' - - | grep Apple
20130210132030 Apple
20130210163415 Apple

Regards.

Last edited by disedorgue; 10-04-2013 at 09:59 AM.. Reason: add grep Apple to fix if last line is date
# 12  
Old 10-04-2013
Coming back to the original request, this might fulfill it:
Code:
awk '/^20131002/ {TMP=$0} /^Voice/ {print TMP" "$0}' file
20131002193434+0500 Voice
20131002193439+0500 Voice

# 13  
Old 10-04-2013
Quote:
Originally Posted by RudiC
Coming back to the original request, this might fulfill it:
Code:
awk '/^20131002/ {TMP=$0} /^Voice/ {print TMP" "$0}' file
20131002193434+0500 Voice
20131002193439+0500 Voice

If date not found and Voice found then print Voice...

Regards.
This User Gave Thanks to disedorgue For This Post:
# 14  
Old 10-04-2013
Rats, you're right! Try (untested):
Code:
awk '/^20131002/ {TMP=$0} /^Voice/ && TMP {print TMP" "$0; TMP=""}' file

This User Gave Thanks to RudiC For This Post:
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