1833870 Members
1676 Online
110063 Solutions
New Discussion

Re: scripting/awk

 
SOLVED
Go to solution
Fred Martin_1
Valued Contributor

scripting/awk

I'm searching a file (/var/mail/inbox) for a particular string, the message ID:

awk '/jA3LQqS29893/{print}' /var/mail/fred

What I really want, is to print the first "Subject:" line that follows a match for the message ID. No way to know how many lines between them.

Any suggestions? Please no perl etc. lets stick to awk.
fmartin@applicatorssales.com
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor
Solution

Re: scripting/awk

Hi Fred:

How about:

awk '/jA3LQqS29893/ {do {getline} while ($0!~/^Subject/);print}'

Regards!

[ perl would look nicer :)) ]

...JRF...
Fred Martin_1
Valued Contributor

Re: scripting/awk

That's beautiful, even for awk, and it works great.

Just noticed though, not all messages have a Subject tag in a mail file. That means there's a possibilty that we hit the end of the file, or the next message '^From ' and we need to terminate, subject or not...

How about that one?
fmartin@applicatorssales.com
Fred Martin_1
Valued Contributor

Re: scripting/awk

I guess the logic would be:

get a line
if end of file
or beginning of next message
then terminate, else
if Subject, print line
fmartin@applicatorssales.com
James R. Ferguson
Acclaimed Contributor

Re: scripting/awk

Hi Fred:

OK, then amend this way, perhaps:

# awk '/jA3LQqS29893/ {do {getline} while ($0!~/^Subject/ && $0!~/^From/);if ($0~/^From/) {exit};print}'

Regards!

...JRF...
Sandman!
Honored Contributor

Re: scripting/awk

IMHO...sed might be better suited for the job-at-hand.
curt larson_1
Honored Contributor

Re: scripting/awk

how about something like this:

awk '/jA3LQqS29893/ {flag = 1;}
/^Subject/ {if (flag == 1) {print;exit;}}
/^From/ {if (flag == 1) {exit;}'
Nguyen Anh Tien
Honored Contributor

Re: scripting/awk

James solution is correct.
You should take time to review awk
pls refer:
http://www.cs.hmc.edu/qref/awk.html
http://www.vectorsite.net/tsawk.html
HP is simple
Fred Martin_1
Valued Contributor

Re: scripting/awk

Many thanks.
fmartin@applicatorssales.com