Computing.Net > Forums > Unix > awk question

awk question

Reply to Message Icon

Original Message
Name: narsman
Date: August 30, 2005 at 16:21:11 Pacific
Subject: awk question
OS: Windows 2000NT
CPU/Ram: 512
Comment:

I need help with awk:

abc 76.33009, 99.88804
abc 76.45004, 98.98344
abc 22.33733, 90.223456
abd 98.7490, 98.870003
abd 12.331113, 98.444501
abe 98.757888, 21.223556

I need the output to group field 1 by it's matching pattern. In other words, I want all abc's together, abd's together, abe's together. The last requirement is to multiply the numbers by 1000 and do not truncate the decimal numbers.

Thanks for your help.


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: August 30, 2005 at 16:34:53 Pacific
Reply: (edit)

You are not being clear by what you mean by grouping "together".


Report Offensive Follow Up For Removal

Response Number 2
Name: narsman
Date: August 30, 2005 at 16:42:16 Pacific
Reply: (edit)

Hi nails,

Sorry if my question is not clear. I want my output to be:

GROUP ABC,
LOC 99888.04/76330.09
LOC 98983.44/76450.04
LOC 90223.456/22337.33
*
GROUP ABD,
LOC 98870.003/98749.0
LOC 98444.501/12331.113
*
GROUP ABE,
LOC 21.223556/98.757888

Please note that the value of the 2nd and 3rd fields are switched.

Thanks again.


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: August 31, 2005 at 11:05:36 Pacific
Reply: (edit)

So, is the data is sorted or not?


Report Offensive Follow Up For Removal

Response Number 4
Name: narsman
Date: August 31, 2005 at 11:35:46 Pacific
Reply: (edit)

Can I have both?


Report Offensive Follow Up For Removal

Response Number 5
Name: nails
Date: August 31, 2005 at 14:02:47 Pacific
Reply: (edit)

No, the only way I can see to do it in awk with an unsorted file is saving the data in a 2-dimensional array and printing it out at the end.

Makefile sure your file is sorted by field one before you start:


sort -k 1,1 myfile|
nawk ' BEGIN { myvar="aa"; fr=1}
{
gsub(",", "",$2) # get rid of the comma
if ($1 != myvar)
{
myvar=$1
if(fr != 1)
print "*"
printf("GROUP %s,\n", $1)
fr=0
}
printf("LOC %8.4f/%8.4f\n", ($3 * 1000), ($2 * 1000))
} '

Since I'm using Solaris, I'm using nawk; change it to awk for non-Solaris.


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 question

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 5 Days.
Discuss in The Lounge