Print 2 matching pattern


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 1 Week Ago
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 1 Week Ago
how about:
Code:
awk '/[/][*]---/,/insert_job/;/max_run_alarm/{print $0 ORS}' myFile

These 2 Users Gave Thanks to vgersh99 For This Post:
bobbygsk (1 Week Ago) nezabudka (1 Week Ago)
# 3  
Old 1 Week Ago
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; 1 Week Ago at 05:35 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
bobbygsk (1 Week Ago) vgersh99 (1 Week Ago)
# 4  
Old 1 Week Ago
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:
bobbygsk (1 Week Ago) Don Cragun (1 Week Ago)
# 5  
Old 1 Week Ago
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 Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Print lines after matching two pattern jhonnyrip Shell Programming and Scripting 4 05-02-2016 05:02 AM
sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern Klasform Shell Programming and Scripting 9 10-12-2015 05:00 PM
Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern essem Shell Programming and Scripting 11 06-05-2014 05:59 AM
Want to print out lines with a matching pattern from file MonikaB Shell Programming and Scripting 2 03-11-2014 07:13 AM
Compare file1 for matching line in file2 and print the difference in matching lines RasB15 Shell Programming and Scripting 2 11-07-2013 10:04 AM
Matching pattern in two files and print the line khalil Shell Programming and Scripting 3 10-17-2013 04:37 AM
How to print all the lines after pattern matching? ankitknit Shell Programming and Scripting 6 07-11-2012 07:01 AM
sed print matching pattern ysrini UNIX for Dummies Questions & Answers 1 02-15-2012 10:56 PM
print range of lines matching pattern and previous line siriche Shell Programming and Scripting 1 02-09-2012 09:00 PM
print contents of file2 for matching pattern in file1 - AWK i.scientist UNIX for Advanced & Expert Users 6 09-08-2009 12:45 AM
Script on pattern matching and print lines and export to excel shaliniyadav Shell Programming and Scripting 34 07-09-2009 02:45 AM
I want to print next 3 lines after pattern matching. naree Shell Programming and Scripting 12 05-21-2009 04:04 AM
Print block of lines matching a pattern vanand420 Shell Programming and Scripting 1 09-29-2008 06:09 AM
Pattern matching in Duplicated file and print once Danish Shakil Shell Programming and Scripting 1 08-01-2008 04:55 AM
pattern matching and print with sed nymus7 Shell Programming and Scripting 2 04-14-2005 10:36 AM