Operating System - HP-UX
1745831 Members
4453 Online
108723 Solutions
New Discussion

Re: awk query (merge field info)

 
SOLVED
Go to solution
Dadski
Advisor

awk or sed query

Morning All,

 

I am trying to extract particular information from a file with multiple fields.  The file appears like so.

 

cat file

gust0101:dmtc:dmtcvg00:497664:1024:32:13
gust0101:wmsc:wmscvg00:847168:64:32:32
gust0101:dmtc:dmtcvg01:470016:1024:32:10
gust0101:wmsc:wmscvg01:361088:64:32:13
gust0102:ap5c:ap5cvg00:69440:64:16:1
gust0103:dp1c:dp1cvg00:55552:64:32:1
gust0104:dp2c:dp2cvg00:345600:512:32:5
gust0201:ss4c:ss4cvg00:707328:256:32:14
gust0201:ss4c:ss4cvg06:262656:128:32:19
gust0201:ss4c:ss4cvg01:2426880:1024:128:43
gust0201:ss4c:ss4cvg02:889344:256:128:16
gust0201:ss4c:ss4cvg03:262656:128:128:19
gust0201:ss4c:ss4cvg04:262656:128:128:19
gust0201:ss4c:ss4cvg05:262656:128:128:19

 

I would like to scan the file and print out the non duplicates in filed 1, and append the second field that matches the first on the same line.

 

I.E gust0201 has 7 records so the following will be printed. along with all of the other lnes.

 

gust0201, ss4cvg00 ,ss4cvg01, ss4cvg02, ss4cvg03, ss4cvg04, ss4cvg05

 

any advice will be welcome.

 

thanks

 

 

P.S. This thread has been moved from HP-UX > General to HP-UX > languages - HP Forums Moderator 

1 REPLY 1
Dennis Handly
Acclaimed Contributor
Solution

Re: awk query (merge field info)

>append the second field

 

Looks like the third field.

 

If the file is sorted on field1, then you could do:

awk -F: '

BEGIN { prev = ""; save="" }

{

if ($1 != prev) {  # diff key

   if (save != "") {

      print save

   }

   prev = $1

   save = $1 "," $3

   next

}

save = save "," $3  # same key

}

END { if (save != "") print save }' input-file