Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combining records

Status
Not open for further replies.

richardii

Programmer
Jan 8, 2001
104
GB
Has anyone written a script to (for example) combine these 5 records:

x1,1,2,3
x1,,,,4,5
x2,1,3,5,7
x2,,,,,9,11
x3,1,2,3,4,5

into these 3:

x1,1,2,3,4,5
x2,1,3,5,7,9,11
x3,1,2,3,4,5

If you have records like this:

x1,2,4
x1,3,5

to combine you'd use the largest value. It's pretty esay in SQL, but I can't do it with GAWK.
 
This works for the example input you provided.



cat yourfile | awk 'BEGIN{ FS = "," }

$1 != prev {
if (NR > 1)
printf("\n")
prev = $1
printf("%s", prev)
for ( i = 2; i <= NF; i++ )
if ( $i != &quot;&quot;)
printf(&quot;,%s&quot;, $i)
next
}

$1 == prev {
for ( i = 2; i <= NF; i++ )
if ( $i != &quot;&quot;)
printf(&quot;,%s&quot;, $i)
}
END { if ( NR > 1 ) printf(&quot;\n&quot;) }' > newfile


Hope this helps!



flogrr
flogr@yahoo.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top