Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with awk if statement 2

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
I am attempting to filter an input file down to just the date and time and the subject. It was working fine until the input changed to a different format for some of the lines.

I was using the following:
Code:
/^Sent:/{$1=""; date = $0}
/^Subject:/{if ($0 ~ "Server")
   $1=""
   print date, $0
}

I tried several different variations including:
Code:
/^Sent:/{$1=""; date = $0}
/^Subject:/{if ($0 ~ "Server")
   $1=""
   print date, $0
else if ($0 !~ "Server"){msg = $2" "$3; next;next;print date, msg, $0}
}

to get it to print 3 lines below Subject if the subject does not contain the string "Server". I have been unsuccessful thus far.

Below is the sample input.

From: abc@def.com
Sent: Thursday, June 03, 2004 8:12 PM
To: def@abc.com
Subject: Job Failed

(Server: "INF05") Job "APPS26 - Diff" failed.

From: abc@def.com
Sent: Thursday, June 03, 2004 6:41 PM
To: def@abc.com
Subject: Backup Exec Alert: Job Failed (Server: "INF08") (Job: "INF22 - Full(Wkly)")

(Server: "INF08") (Job: "INF22 - Full(Wkly)") INF22 - Full(Wkly) -- The job failed with the following error: A communications failure has occurred between the Backup Exec job engine and the remote agent.

Below is the desired output.
Thursday, June 03, 2004 8:12 PM Job Failed (Server: "INF05") Job "APPS26 - Diff" failed.
Thursday, June 03, 2004 6:41 PM Backup Exec Alert: Job Failed (Server: "INF08") (Job: "INF22 - Full(Wkly)")

Any help would be greatly appreciated.

Thanks,

John
 
Try replacing the nexts with getlines

CaKiwi
 
CaKiwi,

I tried

Code:
/^Sent:/{$1=""; date = $0}
/^Subject:/{if ($0 ~ "Server")
   $1=""
   print date, $0
else if ($0 !~ "Server"){msg = $2" "$3; next;getline subject;print date, msg, subject}
}

but no luck. Any other suggestions.

Thanks,

John
 
You may try something like this:
/^Sent:/{$1=""; date = $0; f=1; msg="";next}
/Subject/,/Server/{if(f)msg=msg""$0;f=2;next}
f==2{sub(/^Subject:/,"",msg);sub(/^[ \t]+/,"",msg);print date" "msg;f=0}

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
change all of your 'next' to 'getline'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
PHV,

Thank you, your solution worked!!

Vlad / CaKiwi,

I couldn't get it to work using the [/b]getline[/b]s.

I tried
Code:
else if ($0 !~ "Server"){msg = $2" "$3; getline;getline subject;print date, msg, subject}

and

Code:
else if ($0 !~ "Server"){msg = $2" "$3; getline;getline;print date, msg, $0}

to no avail.

Thank you all for your help.

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top