Computing.Net > Forums > Unix > Awk help Pls

Awk help Pls

Reply to Message Icon

Original Message
Name: cpvinay
Date: July 7, 2008 at 08:50:43 Pacific
Subject: Awk help Pls
OS: XP
CPU/Ram: Intel P 1GB
Model/Manufacturer: Dell
Comment:

I need some help in extracting data from multiple email messages.

The data looks somewhat like this

User Name: abc
SubDate:070608
CompDate:080608
ItemID1:123
ItemID2:345
ItemID3:678
Remote_Host:111.45.122.65


User Name: xyz
SubDate:050608
CompDate:070608
ItemID1:478
ItemID2:258
Remote_Host:120.22.222.65

and it continues like that. I need the output as
abc,070608,080608,123
abc,070608,080608,345
abc,070608,080608,678
xyz,050608,070608,478
xyz,050608,070608,258

Is it possible to get this kind out output using awk. Please help

Thanks


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: July 7, 2008 at 11:36:07 Pacific
Reply: (edit)

One way ....

Since I'm using Solaris, I'm using nawk:


#!/bin/ksh

# no error checking
nawk ' BEGIN { FS=":" }
{
if ($1 == "User Name")
{
gsub(" ", "") # get rid of space
string=$2"," # build the string
getline # get next line
string=string$2","
getline
string=string$2","
getline
string=string$2
print string
}

} ' data.file



Report Offensive Follow Up For Removal

Response Number 2
Name: cpvinay
Date: July 8, 2008 at 06:24:47 Pacific
Reply: (edit)

Thanks for the reply! But the output I got had only the first lines from each record
abc,070608,080608,123
xyz,050608,070608,478

Another problem that I have is that input file,since it is extracted from email contains lot of unwanted stuff like msg body which I dont need to extract.


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: July 8, 2008 at 07:35:40 Pacific
Reply: (edit)

This assumes that each block starts with "User Name" and ends with "Remote_Host"


#!/bin/ksh

nawk ' BEGIN { FS=":" }
{
if ($1 == "User Name" )
{
gsub(" ", "") # get rid of space
string=$2","
getline # get next line
string=string$2","
getline
string=string$2","
getline
while ($1 != "Remote_Host")
{
if ($1 ~ /^Item/)
print string$2
getline
}
}

} ' datafile



Report Offensive Follow Up For Removal

Response Number 4
Name: ghostdog
Date: July 8, 2008 at 09:58:14 Pacific
Reply: (edit)

GNU awk

awk 'BEGIN{RS="";FS=":";OFS=","}
{
for(i=1;i<=NF;i++) {
if ( $i ~ /ItemID/) {
print $2,$4,$6,$(i+1)
}
}
}' file



Report Offensive Follow Up For Removal

Response Number 5
Name: cpvinay
Date: July 9, 2008 at 09:50:23 Pacific
Reply: (edit)

Thanks nails and ghostdog...I could build on from the suggestions you have provided...Appreciate your help!


Report Offensive Follow Up For Removal


Response Number 6
Name: davidcvu
Date: September 8, 2008 at 15:40:40 Pacific
Reply: (edit)

Hi all,

Just wonder if you could help. I have a similar problem but in this case, I'd need to modify the script in order to grap just the first line and the last line of each block.

For each block of information, all I need is to display the first and the last row of each block (and skipping other data in the middle).
For example,

User Name:
Remote_Host

Thanks very much for your help.
David


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Awk help Pls

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge