Search for patterns on different lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for patterns on different lines
# 1  
Old 03-23-2017
Search for patterns on different lines

im using the following code to search a log for entries on two different lines:

Code:
awk 'BEGIN{count=0} /'"${firstpattern}"'/,/'"${secondpattern}"'/ { print; if ($0 ~ /'"${thirdpattern}"'/){count++}; } END { print count }' data.txt


Code:
firstpattern="start error log"
secondpattern="i am logging the errors now"
thirdpattern="App crashed"

data.txt

Code:
blahblahblahblahahahahahahahahahahahlalalalalalalalalal
auffkffiaooojf jafkaf kafkakf akkkkkkkkkkkfaf kafkafk akfkafka
March 22 2017 start error log
today is lovely.  it is not raining. No snow.  fantastic
......
I am logging the errors now
tomorrow. it will rain. stay in doors.
App crashed
.......
I am fine today. Just dont bother me.  
Im watching a movie tomorrow.  You;re not invited.

Now, when the above awk code is run on the data.txt file, it will pull out only the text im interested in, which is:

Code:
March 22 2017 start error log
today is lovely.  it is not raining. No snow.  fantastic
......
I am logging the errors now
tomorrow. it will rain. stay in doors.
App crashed

sometimes, there will be many instances where multiple "start error log" are present. but neither "I am logging the errors now" nor "App crashed" strings will be present. there will be other errors in their place, errors i dont care about. in these instances, the errors that I do care about "I am logging the errors now" and "App crashed" are usually towards the bottom under one of the other "start error log" strings.

so what ends up happening is that, the awk code tries to print everything it finds from the first "start error log" instance, to the last.

i want the awk code to skip printing an entire chunk of lines if the three patterns im looking for are not all there.
# 2  
Old 03-23-2017
It seems in your script have pattern 2 and 3 reversed, no?
If so, try something like this:
Code:
awk -v p1="start error log" -v p2="I am logging the errors now" -v p3="App crashed" '
  s!="" {
    s=s RS $0
    if($0~p3) {
      if (s~p2)
        print s
      s=""
    }
  } 
  $0~p1 {
    s=$0
  }
' data.txt


---
Also in you sample secondpattern variable, the "I" was written as "i"
This User Gave Thanks to Scrutinizer 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

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. UNIX for Beginners Questions & Answers

Delete multiple lines between blank lines containing two patterns

Hi all, I'm looking for a way (sed or awk) to delete multiple lines between blank lines containing two patterns ex: user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 16... (3 Replies)
Discussion started by: ce9888
3 Replies

3. Shell Programming and Scripting

Search and count patterns

Hi, I have a text file the contents are like this now i want to search patterns Z , Z etc and count the occurrence of such patterns, after Z value can be any random digits, please help me it is urgent... output like this Z .............>5 Z ............>8 (9 Replies)
Discussion started by: sreejithalokkan
9 Replies

4. Shell Programming and Scripting

How to search multiple patterns and remove lines from a file?

Hi, I have a file content as below. Table : PAYR Displayed fields: 15 of 15 Fixed columns: 4 List width 0999... (4 Replies)
Discussion started by: shirdi
4 Replies

5. Shell Programming and Scripting

Search for the two patterns and print everything in between

Hi all, I have a file having data: @HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTAATA NTTGGGTTTTCT @HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTA... NTTGGGTTTTCT @HWUSI-EAS1727:19:6:1:3674:984:0:1#.....CT NTTGGGTTTTCT I want to print everything starting from # till line ends. can you please help me how... (5 Replies)
Discussion started by: pirates.genome
5 Replies

6. Shell Programming and Scripting

reading lines from a file between two search patterns

Hi, I am new to shell scripting and is working on a script to extract lines from a log file between two time stamps using awk command. After some research I used following command: awk '/01 Oct 2011/{p=1} /10 Oct 2011/{p=0} p' test.log >> tmp.log This works fine. But now i want to... (3 Replies)
Discussion started by: davidtd
3 Replies

7. UNIX for Dummies Questions & Answers

Printing the lines using search patterns

Hi all , i need an help here.!!!! i have a file that contains /etc/passwd files from some servers. i need a script which search for presence of a user in the servers. like if i give 51144 to the script. the should be o/p Please help on this..... (4 Replies)
Discussion started by: sudharson
4 Replies

8. Shell Programming and Scripting

search multiple patterns

I have two lists in a file that look like a b b a e f c d f e d c I would like a final list a b c d e f I've tried multiple grep and awk but can't get it to work (8 Replies)
Discussion started by: godzilla07
8 Replies

9. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

10. Shell Programming and Scripting

search patterns

hello, i have an input file of about 50,00,000 lines. few of its lines are as follows: <CR:0023498789,TPO-14987084;BO=IC&SUB=ALLP <CF:0023498789,CB=YES;BIL&NC=NO <CF:0023498789,CW=NO;NS=NO <GC:0023498789,CG=YES;TPO&NC=YES <CR:0024659841,TPO-14484621;BO=NO&BA=OC&SUB=ALLH... (1 Reply)
Discussion started by: rochitsharma
1 Replies
Login or Register to Ask a Question