0

The situation is next. I got log file where logs separated with minus chars like:

Timestamp1
---
Log: 1
Address: http://addr1.com
Payload: <soap:Envelope>
             <soap:Body>
                 <context 1-1>
                 <context 1-2>
                 <context 1-3>
             </soap:Body>
         <soap:Envelope>
---;
Timestamp2
---
Log: 2
Address: http://addr2.com
Payload: <soap:Envelope>
             <soap:Body>
                 <context 2-1>
             </soap:Body>
         <soap:Envelope>
---;
Timestamp3
---
Log: 3
Address: http://addr3.com
Payload: <soap:Envelope>
             <soap:Body>
                 <context 3-1>
                 <context 3-2>
             </soap:Body>
         <soap:Envelope>
---;
...

I need to get whole log info where some keyword found e.g. if keyword is "context 2-1" next strings should be printed:

---
Log: 2
Address: http://addr2.com
Payload: <soap:Envelope>
             <soap:Body>
                 <context 2-1>
             </soap:Body>
         <soap:Envelope>
---;

So how can i do this pattern search with "greedy" cut of delimeters around it?

Vadim K
  • 129
  • 10

3 Answers3

2

With sed:

sed -n '/^---/ {:a;N;/---;/!ba;/context 2-1/p}' file

Explanation:

  • /^---/ when line starting with --- is found
  • a: label for to come loop
  • N: add next line to the pattern space
  • /---;/!: while ---; if not found...
  • ba loops to a label
  • /context 2-1/p: when loop terminates, print all lines previously added to pattern space if context 2-1 is found
SLePort
  • 15,211
  • 3
  • 34
  • 44
0

Using this as a guideline: How to select lines between two patterns?

$0=="---" {                   # at the fron marker
    flag=1                    # flag up
    buf=""                    # clear the buf
} 
$0=="---;" { flag=0 }         # at the end marker, flag down
{
    buf=buf $0 (flag?RS:"")   # gather buffer, add RS before the end marker
    if($0 ~ "^Payload2")      # if keyword found
        output=1              # output flag up
} 
flag==0 && output==1 {        # after end marker when putput flag up
    print buf                 # output buf
    output=0                  # output flag down
}

Run it:

$ awk -f script.awk logfile
---
Log2
Address2 ...
Payload2 ...
---;
Community
  • 1
  • 1
James Brown
  • 36,089
  • 7
  • 43
  • 59
  • Thank you for the answer. Yes, i know how to cut text between patterns using sed, but i didn't know how to check another existence of second pattern. – Vadim K Oct 10 '16 at 15:08
0
awk -vRS="Timestamp[0-9]+"  -v k="context 2-1"  '$0~k' file2

This uses Timestamp[0-9]+ as a new-line character. k is keyword what you want. If $0 matches the keyword, then print $0.

Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
zxy
  • 148
  • 1
  • 2