Print 2 matching pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Print 2 matching pattern
# 1  
Old 02-09-2019
Print 2 matching pattern

How to a first 3 lines lines that matches 1st pattern and 1 line that matches 2nd line.

Sample.txt
Code:
Line 1: /*--- ABC_BA_ABCABC -----*/
Line 2:
Line 3: insert_job: ABC_BA_ABCABC job_type: BOX
Line 4: blah blah
Line 5: max_run_alarm: 5
Line 6: blah blah
----
-----
Line 20: /*--- BCD_CA_BCDBCD -----*/
Line 21:
Line 22: insert_job: BCD_CA_BCDBCD job_type: CMD
Line 23: blah blah
Line 24: blah blah
Line 25: blah blah
Line 26: max_run_alarm: 5
Line 27: blah blah
--
--
--
Line 50: /*--- CDE_CA_CDECDE -----*/
Line 51:
Line 52: insert_job: CDE_CA_CDECDE job_type: CMD
Line 53: blah blah
Line 54: blah blah
Line 57: max_run_alarm: 5
Line 58: blah blah

I could get first 3 lines using grep -B 3 'insert_job'
The lines between 1st pattern and 2nd pattern are not consistent.

Need output as shown below
Code:
Line 1: /*--- ABC_BA_ABCABC -----*/
Line 2:
Line 3: insert_job: ABC_BA_ABCABC job_type: BOX
Line 5: max_run_alarm: 5

Line 20: /*--- BCD_CA_BCDBCD -----*/
Line 21:
Line 22: insert_job: BCD_CA_BCDBCD job_type: CMD
Line 26: max_run_alarm: 5

Line 50: /*--- CDE_CA_CDECDE -----*/
Line 51:
Line 52: insert_job: CDE_CA_CDECDE job_type: CMD
Line 57: max_run_alarm: 5

Ignore Line#n. That is to show that the lines between pattern 1 and pattern 2 are not consistent

Appreciate your help.
# 2  
Old 02-09-2019
how about:
Code:
awk '/[/][*]---/,/insert_job/;/max_run_alarm/{print $0 ORS}' myFile

These 2 Users Gave Thanks to vgersh99 For This Post:
# 3  
Old 02-09-2019
Note: it is best to escape the / inside the square bracket expression when used inside a regex constant, to prevent it from being confused with the closing / of the regex constant. Some awks cannot handle that.
Code:
awk '/[\/][*]---/,/insert_job/...

or
Code:
awk '/\/[*]---/,/insert_job/...

or
Code:
awk '$0~"/[*]---",/insert_job/...


Last edited by Scrutinizer; 02-10-2019 at 05:35 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 4  
Old 02-09-2019
Quote:
Originally Posted by bobbygsk
Code:
Line 1: /*--- ABC_BA_ABCABC -----*/
Line 2:
Line 3: insert_job: ABC_BA_ABCABC job_type: BOX
Line 4: blah blah
Line 5: max_run_alarm: 5
Line 6: blah blah
----
-----
Line 20: /*--- BCD_CA_BCDBCD -----*/
Line 21:
Line 22: insert_job: BCD_CA_BCDBCD job_type: CMD
Line 23: blah blah
Line 24: blah blah
Line 25: blah blah
Line 26: max_run_alarm: 5
Line 27: blah blah
--
--
--
Line 50: /*--- CDE_CA_CDECDE -----*/
Line 51:
Line 52: insert_job: CDE_CA_CDECDE job_type: CMD
Line 53: blah blah
Line 54: blah blah
Line 57: max_run_alarm: 5
Line 58: blah blah

Need output as shown below
Code:
Line 1: /*--- ABC_BA_ABCABC -----*/
Line 2:
Line 3: insert_job: ABC_BA_ABCABC job_type: BOX
Line 5: max_run_alarm: 5

Line 20: /*--- BCD_CA_BCDBCD -----*/
Line 21:
Line 22: insert_job: BCD_CA_BCDBCD job_type: CMD
Line 26: max_run_alarm: 5

Line 50: /*--- CDE_CA_CDECDE -----*/
Line 51:
Line 52: insert_job: CDE_CA_CDECDE job_type: CMD
Line 57: max_run_alarm: 5

Ignore Line#n. That is to show that the lines between pattern 1 and pattern 2 are not consistent
Ignoring the line numbers is OK, but right now there is a number, then a colon and then a space. Only then the text begins. I am not sure if the space is in your data or not, but suppose it isn't. If it is there you will have to adjust the following regexps accordingly.

Code:
sed -n '/^\/\*---/ {N;N;p};/^max_run_alarm:/p' /path/to/your/file

Your partial solution with grep -B should be avoided: "-B" is a non-standard extension to the standard grep and you can never be sure if the grep version of every system is able to understand that. It is a hard (and usually painful) lesson you learn from administrating large datacenters with hundreds or even thousands of various systems that you stick to the standards religiously because that is basically what system is "guaranteed to understand".

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
# 5  
Old 02-11-2019
Thank you - vgersh99, Scrutinizer, bakunin

Earlier I had to do it in 4-5 steps. Now I can do it in 1 easy step, thanks to you all
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print lines after matching two pattern

would like to print everything after matching two patterns AAA and BBB. output : CCC ZZZ sample data : AAA BBB CCC ZZZ (4 Replies)
Discussion started by: jhonnyrip
4 Replies

2. Shell Programming and Scripting

Want to print out lines with a matching pattern from file

Hi all, I want to search for strings in file1 that can be found in file2 and print out the whole line when matching pattern is found. I have used the below command, but this is not working for me, because it is writing out only the matching patterns from file2, not the whole line. fgrep -o... (2 Replies)
Discussion started by: MonikaB
2 Replies

3. Shell Programming and Scripting

Matching pattern in two files and print the line

Hi, I want to match the pattern in file1 with file2 and print the value in file2 and paste in file1 file1: ISHO RT SR Major 96.46778 Drop Call Rate CS Critical 0.5072662 ISHO RT SR Major 97.754364... (3 Replies)
Discussion started by: khalil
3 Replies

4. Shell Programming and Scripting

How to print all the lines after pattern matching?

I have a file that contains... Number -------------------- 1 2 3 4 i want to print all the numbers after the hyphen ... (6 Replies)
Discussion started by: ankitknit
6 Replies

5. UNIX for Dummies Questions & Answers

sed print matching pattern

Hi, i have data file like: START1 a b STOP c d START2 e STOP f START3 g STOP When one of the START<count> variable is passed, i should print all lines matching this until the first 'STOP' for example if 'START2' is provided for match, i should get the result as: START2 (1 Reply)
Discussion started by: ysrini
1 Replies

6. Shell Programming and Scripting

Script on pattern matching and print lines and export to excel

Hi Friends, I am working on a script.. Looking forward for your expert help..... My requirement is: I have a text file where, need to search equip * RTF or end of line with RTF ,once this pattern is found then print 2nd line, 6th line, 7th line to a different file. For Ex: equip 1... (34 Replies)
Discussion started by: shaliniyadav
34 Replies

7. Shell Programming and Scripting

I want to print next 3 lines after pattern matching.

Dear Experts, I have file called file1 in which i am greping a pattern after that i want to next 3 lines when that pattern is matched. Ex:- file1 USA UK India Africa Hello Asia Europe Australia Hello Peter Robert Jo i want to next 3 lines after matching Hello... (12 Replies)
Discussion started by: naree
12 Replies

8. Shell Programming and Scripting

Print block of lines matching a pattern

Hi :), I am using the script to search "MYPATTERN" in MYFILE and print that block of lines containing the pattern starting with HEADER upto FOOTER. But my problem is that at some occurrence my footer is different e.g. ";". How to modify the script so that MYPATTERN between HEADER and different... (1 Reply)
Discussion started by: vanand420
1 Replies

9. Shell Programming and Scripting

Pattern matching in Duplicated file and print once

Dear Experts, I have many alarms appeared in a file twice, i want to grep them with this info EVTTIME & DOMAIN, and print them in second file with 1 occurance. I have tried uniq -d test.txt > newfile and awk '!arr++' test.txt > newfile both are not working Please help me with this!!! ... (1 Reply)
Discussion started by: Danish Shakil
1 Replies

10. Shell Programming and Scripting

pattern matching and print with sed

Hi It is possible with sed to print a pattern within a line matching regexp? So, the line looks like : 19:00:00 blablablabla jobid 2345 <2> the regexp is "jobid 2345" and the pattern is 56434. That the code for find... (2 Replies)
Discussion started by: nymus7
2 Replies
Login or Register to Ask a Question