How to get line after occurence of sequence of patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get line after occurence of sequence of patterns
# 1  
Old 10-12-2011
How to get line after occurence of sequence of patterns

In the past I needed a help with the problem how to search for pattern after the occurence of another pattern which is described in this thread:

https://www.unix.com/shell-programmin...-pattern1.html

Now I would need something quite similar, only the pattern which is to be searched must be preceded by occurence of more patterns (in given order), not just by one.

For example:

Code:
any lines
pattern1
any lines
pattern3
...
anylines
pattern1
any lines
pattern2
any lines
pattern3
any lines
pattern4
line1
line2
the line I need to get
any lines

I need to get the third line after the occurence of the pattern4 but only if pattern1, pattern2 , pattern3 has occured before in given order!.

Would you help me please?

Thanks

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by sameucho; 10-12-2011 at 08:17 AM..
# 2  
Old 10-12-2011
I suppose you meant the output should be line2?

Try this...
Code:
sed -n 'H;/pattern4/{x;/pattern1.*pattern2.*pattern3/{n;n;p}}' input_file

--ahamed
# 3  
Old 10-12-2011
Quote:
Originally Posted by ahamed101
I suppose you meant the output should be line2?

Try this...
Code:
sed -n 'H;/pattern4/{x;/pattern1.*pattern2.*pattern3/{n;n;p}}' input_file

--ahamed

I mean the output should be "the line I need to get". The example wasn't originaly quoted correctly (meantime I have corrected it). But if I understand your proposal well, only one more ';n' should be added to 'print' section of your sed script.

Anyhow, I don't understand your script well - would you explain please the meaning of 'H;', 'x;' and why pattern2 & pattern3 are preceded by asterisk ('*')?

Thanks
# 4  
Old 10-13-2011
yes you are right, add one more n

Code:
sed -n 'H;/pattern4/{x;/pattern1.*pattern2.*pattern3/{n;n;n;p}}' input_file

--ahamed

---------- Post updated at 08:44 PM ---------- Previous update was at 07:06 AM ----------

H : Hold the incoming data into hold buffer.
When you find pattern4, then get the held data into current space (using x - exchange) and search if pattern1, pattern2 and pattern3 are present and in the same order.
If present, read 3 more lines(using n) and print it.

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Vi delete line with second occurence of pattern

I have a large file and many lines are duplicated. I'm trying to delete lines with every second occurrence of a pattern. Did tried searching similar question but no luck. I can delete all lines matching pattern with :g/pattern/d but don't want to lose data. Sample pattern to delete... (6 Replies)
Discussion started by: homer4all
6 Replies

2. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

3. UNIX for Advanced & Expert Users

Get the first occurence between two patterns

I have an output file which gives me the timely status of a server. Sample file: March 11 2014 21:10, 1, 2, 3, 4, 5, 6, 7, 8, 9, x, y, z... 21:05, 1, 2, 3, 4, 5, 6, 7, 8, 9, x, y, z... 21:00, 1, 2, 3, 4,... (3 Replies)
Discussion started by: rpm120
3 Replies

4. Shell Programming and Scripting

Grab nth occurence in between two patterns using awk or sed

Hi , I have an issue where I want to parse through the output from a file and I want to grab the nth occurrence of text in between two patterns preferably using awk or sed ! TICKET NBR : 1 !GSI : 102 ! 3100.2.112.1 11/06/2013 15:56:29 ! 3100.2.22.3 98 ! 3100.2.134.2... (8 Replies)
Discussion started by: OTNA
8 Replies

5. Shell Programming and Scripting

Print between patterns - first occurence, second occurence etc

I have a file # cat asasas AAAAAA 11 22 33 44 BBBBB NILNILNIL AAAAAA 22 33 44 55 66 77 88 BBBBB NILNILNIL (2 Replies)
Discussion started by: anil510
2 Replies

6. Shell Programming and Scripting

sed print between 2 patterns only last occurence

Hi, I have a file, which contains the following log data. I am trying to print fromt he file the following data: I have tried using sed, but I am getting from the first pattern Thanks for your help. (5 Replies)
Discussion started by: sol_nov
5 Replies

7. Shell Programming and Scripting

Retrieve lines that match any occurence in a list of patterns

I have two files. The first containing a header and six columns of data. Example file 1: Number SNP ID dbSNP RS ID Chromosome Result_Call Physical Position 787066 SNP_A-8575395 RS6650104 1 NOCALL 564477 786872 SNP_A-8575125 RS10458597 1 AA ... (13 Replies)
Discussion started by: Selftaught
13 Replies

8. UNIX for Dummies Questions & Answers

How to count the occurence of a character in a line

Suppose i have data like :- 1,2,3,4,5 a,b,c x,y,z,t I want to count the occurence of , (comma) in every line. Waiting for a solution. (5 Replies)
Discussion started by: sumit207
5 Replies

9. UNIX for Dummies Questions & Answers

Counting occurence of a particular character on each line

Hi, I have the following data in a flat file: abcd_efgh_ijkl_20080522.dat|20080602222508|1357 abcd_efgh_ijkl_20080522.dat|20080602222508|1357 abcd_efgh_ijkl_20080522.dat|20080602222508|1357 I need to check the no. of occurence of "|" (pipe) on each line and the output should look like below:... (4 Replies)
Discussion started by: hey_mak
4 Replies

10. Shell Programming and Scripting

first occurence and line number

Hi, My objective is to get the line number of the first occurance of the search pattern. my test.txt contains: ..... .................. total rows.... ................... .. total rejected rows: 40 total rejected rows: 50 total rejected rows: 80 total rejected rows: 90 total... (9 Replies)
Discussion started by: mercuryshipzz
9 Replies
Login or Register to Ask a Question