Computing.Net > Forums > Unix > awk question

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

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:

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:

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:

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:

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:

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







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



Results for: awk question

awk question
    Summary: Here's what goes on in the script... The results of grep are assigned to grp1 and grp2. $grp1 is 1file08[1] 1file04[2] 1file05[3] 1file09[4] $grp2 is 2file08[1] 2file05[2] 2file09[3] I put them in a ...
www.computing.net/answers/unix/awk-question/6685.html

AWK Question
    Summary: Thank you for your response on my previous question on "Reformatting the month of year"! I have another question regarding passing a variable to awk command. I have a file with file lablels in it, fo...
www.computing.net/answers/unix/awk-question/4512.html

sed awk question
    Summary: A question for ya. Using sed or awk or both. Below is an example of a file. These are accounts. From the record PT to the next PT is one account. I want to strip off the carriage return & line fe...
www.computing.net/answers/unix/sed-awk-question/6900.html








Which MP3 player do you have?

iPod/iPhone
Zune
Something Else
None


View Results

Poll Finishes Today.
Discuss in The Lounge
Poll History






Data Recovery Software