How to print line if two lines above it matches patterns.?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print line if two lines above it matches patterns.?
# 1  
Old 10-22-2015
How to print line if two lines above it matches patterns.?

Hi,

I could only find examples to print line before/after a match, but I'd need to print line after two separate lines matching.

E.g.: From the below log entry, I would need to print out the 1234. This is from a huge log file, that has a lot of entries with "CLIENT" and "No" entries (+ other messages), and I need to print the client number only if the "CLIENT" and "No" tags are in consecutive lines as in the below example.

Would anyone have idea how to do this (awk/sed/?)

Code:
....
[12:16:30.901]  <CLIENT>
[12:16:30.901]          <No>
[12:16:30.901]          1234
[12:16:30.901]          </No>
[12:16:30.901]  </CLIENT>
....

# 2  
Old 10-22-2015
Let me try to help with my limited knowledge Smilie

Code:
grep -B1 No log_file | grep -A2 CLIENT | tail -n1

Try that. Hope it helps.
Regards

Last edited by rk4k; 10-22-2015 at 05:19 AM.. Reason: not complete
# 3  
Old 10-22-2015
Try sed:
Code:
sed -n '/<CLIENT>/{n;/<No>/{n;p;};}' file

output:
Code:
[12:16:30.901]          1234



---
Edit: or a fun awk:
Code:
awk '/<CLIENT>/ && getline && /<No>/ && getline' file


Last edited by Scrutinizer; 10-22-2015 at 04:52 AM..
# 4  
Old 10-22-2015
Perhaps you want something like:
Code:
awk '
$2 == "<CLIENT>" {
	m = 1
	next
}
m == 1 && $2 == "<No>" {
	m = 2
	next
}
m == 2 {print $2
}
{	m = 0
}' file

If file contains:
Code:
[12:16:30.901]  <CLIENT>
[12:16:30.901]          <No>
[12:16:30.901]          1234
[12:16:30.901]          </No>
[12:16:30.901]  </CLIENT>
[12:16:30.901]  <CLIENT>
[12:16:30:901]          <NotNo>
[12:16:30.901]          1235
[12:16:30.901]          </NotNo>
[12:16:30.901]  </CLIENT>
[12:16:30.901]  <CLIENT>
[12:16:30:901]          <NotNo>
[12:16:30.901]          1236
[12:16:30.901]          </NotNo>
[12:16:30.901]          <No>
[12:16:30.901]          1237
[12:16:30.901]          </No>
[12:16:30.901]  </CLIENT>
....

it produces the output:
Code:
1234

which, I believe, matches what you said you wanted.

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

Egrep patterns in a file and limit number of matches to print for each pattern match

Hi I need to egrep patterns in a file and limit number of matches to print for each matched pattern. -m10 option is not working out in my sun solaris 5.10 Please guide me the options to achieve. if i do head -10 , i wont be getting all pattern match results as output since for a... (10 Replies)
Discussion started by: ananan
10 Replies

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

5. Shell Programming and Scripting

awk to print the line that matches and the next if line is wrapped

I have a file and when I match the word "initiators" in the first column I need to be able to print the rest of the columns in that row. This is fine for the most part but on occasion the "initiators" line gets wrapped to the next line. Here is a sample of the file. caw-enabled ... (3 Replies)
Discussion started by: kieranfoley
3 Replies

6. Shell Programming and Scripting

Using regex's from file1, print line and line after matches in file2

Good day, I have a list of regular expressions in file1. For each match in file2, print the containing line and the line after. file1: file2: Output: I can match a regex and print the line and line after awk '{lines = $0} /Macrosiphum_rosae/ {print lines ; print lines } ' ... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

7. Shell Programming and Scripting

Print all lines between patterns

Hi Gurus, I have a requirement where I need to display all lines between 2 patterns except the line where the first pattern in it. I tried the following command using awk but it is printing all lines except the lines where the 2 patterns exist. awk '/TRANSF_/{ P=1; next } /Busy/ {exit} P'... (9 Replies)
Discussion started by: svajhala
9 Replies

8. Shell Programming and Scripting

How to print only lines in between patterns?

Hi, I want to print only lines (green-italic lines) in between first and last strings in column 9. there are different number of lines between each strings. 10 AUGUSTUS exon 4558 4669 . - . 10.g1 10 AUGUSTUS exon 8771 8889 . ... (6 Replies)
Discussion started by: jamo
6 Replies

9. Shell Programming and Scripting

Need to print between patterns AND a few lines before

I need to print out sections (varying numbers of lines) of a file between patterns. That alone is easy enough: sed -n '/START/,/STOP/' I also need the 3 lines BEFORE the start pattern. That alone is easy enough: grep -B3 START But I can't seem to combine the two so that I get everything between the... (2 Replies)
Discussion started by: Finja
2 Replies

10. Shell Programming and Scripting

How to print line if field matches?

Hi all, I got several lines line this a b c d e 1 e a 1 c d e 3 f a b c 1 e 8 h a b c d e 1 w a 1 c d e 2 w a b c d e 1 t a b c d e 7 4 How can I print the line if 1 is the field one before the last field? Basicly this 2 field ? a b c d e 1 e a b c d e 1 t The file I got is... (7 Replies)
Discussion started by: stinkefisch
7 Replies
Login or Register to Ask a Question