Computing.Net > Forums > Unix > awk question

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

awk question

Reply to Message Icon

Name: narsman
Date: August 30, 2005 at 16:21:11 Pacific
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.



Sponsored Link
Ads by Google

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

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


0

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

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.


0

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

So, is the data is sorted or not?


0

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

Can I have both?


0

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

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.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: awk question

awk question www.computing.net/answers/unix/awk-question/6685.html

AWK Question www.computing.net/answers/unix/awk-question/4512.html

sed awk question www.computing.net/answers/unix/sed-awk-question/6900.html