views:

65

answers:

2
+1  Q: 

Extract log file

Hi,

xxxxxxxxmessageyyyyyyymessagexxxxxxxxxx

xxxxxxxxmessagezzzzzzzmessagexxxxxxxxxx

xxxxxxxxmessageaaaaaaamessagexxxxxxxxxx

xxxxxxxxmessageyyyyyyymessagexxxxxxxxxx

The above is my log file I need to extract the phrase which is inside the message tag and I need to save the distinct messages in a file in the above example I need to save zzzzzzz and aaaaaaa to a file.

What are the unix commands I need to use.

A: 

awk:

BEGIN
{
  s=0
}

s==0 && /yyyyy/
{
  s=1
  next
}

s==1 && /yyyyy/
{
  exit
}

{
  print
}

Run with awk -f script.awk < infile > outfile.

Ignacio Vazquez-Abrams
A: 

There are a dozen ways to accomplish this. The one I tested:

(sed 's/xx//g' | sed 's/message//g')<tstlog >processed.log

yielded

yyyyyyy

zzzzzzz

aaaaaaa

yyyyyyy

Also, if you want to omit the blank lines, you could add something like:

(sed 's/xx//g' | sed 's/message//g' | grep -v ^$) < tstlog >processed.log
MattyV