Home > Archive > AWK > January 2008 > concatenating 2nd field if 1st field is same
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
concatenating 2nd field if 1st field is same
|
|
| sixstringsin@yahoo.co.in 2008-01-13, 6:59 pm |
| I have a datafile:
data1:1,2,3,4
data2:434,434,233,7
data3:23,44,76,334
data4:4,6,55,8
data1:6,76,45,09
data5:45,43,343,2
data2:3,43,5,5656,5
I am using awk with input field separator as ":"
I want to concatenate field 2 of every record that has the same field
1.
The final contents of the processed file should look like:
data1:1,2,3,4,6,76,45,09
data2:434,434,233,7,3,43,5,5656,5
data3:23,44,76,334
data4:4,6,55,8
data5:45,43,343,2
Any guidance is highly appreciated.
| |
| Janis Papanagnou 2008-01-13, 6:59 pm |
| sixstringsin@yahoo.co.in wrote:
> I have a datafile:
>
> data1:1,2,3,4
> data2:434,434,233,7
> data3:23,44,76,334
> data4:4,6,55,8
> data1:6,76,45,09
> data5:45,43,343,2
> data2:3,43,5,5656,5
>
>
> I am using awk with input field separator as ":"
>
> I want to concatenate field 2 of every record that has the same field
> 1.
>
>
> The final contents of the processed file should look like:
>
> data1:1,2,3,4,6,76,45,09
> data2:434,434,233,7,3,43,5,5656,5
> data3:23,44,76,334
> data4:4,6,55,8
> data5:45,43,343,2
>
>
> Any guidance is highly appreciated.
This is one possibility...
awk -F: '{ a[$1] = a[$1] "," $2 }
END { for (d in a) print d ":" substr(a[d],2) }
' file
(It has the drawback to produce unordered results, though;
if that's a problem come back and tell us...)
Janis
| |
|
| sixstringsin@yahoo.co.in said the following on 1/13/2008 12:30 PM:
> I have a datafile:
>
> data1:1,2,3,4
> data2:434,434,233,7
> data3:23,44,76,334
> data4:4,6,55,8
> data1:6,76,45,09
> data5:45,43,343,2
> data2:3,43,5,5656,5
>
>
> I am using awk with input field separator as ":"
>
> I want to concatenate field 2 of every record that has the same field
> 1.
>
>
> The final contents of the processed file should look like:
>
> data1:1,2,3,4,6,76,45,09
> data2:434,434,233,7,3,43,5,5656,5
> data3:23,44,76,334
> data4:4,6,55,8
> data5:45,43,343,2
>
>
> Any guidance is highly appreciated.
awk -F":" '{r[$1]=r[$1] $2}END{for(i in r) print i":"r[i]}' file
--
(^\pop/^)
I Stopped to think but forgot to start again.
--
| |
| sixstringsin@yahoo.co.in 2008-01-13, 6:59 pm |
| On Jan 13, 11:46 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
> sixstring...@yahoo.co.in wrote:
>
>
>
>
>
>
>
> This is one possibility...
>
> awk -F: '{ a[$1] = a[$1] "," $2 }
> END { for (d in a) print d ":" substr(a[d],2) }
> ' file
>
> (It has the drawback to produce unordered results, though;
> if that's a problem come back and tell us...)
>
> Janis
thanks so much Janice..the order is not relevant. This works
purrrfffectly.
<sincere apologies for the double post, that wasnt intentional.>
| |
| sixstringsin@yahoo.co.in 2008-01-13, 6:59 pm |
|
>
> awk -F":" '{r[$1]=r[$1] $2}END{for(i in r) print i":"r[i]}' file
>
> --
> (^\pop/^)
> I Stopped to think but forgot to start again.
>
thanks a lot!
|
|
|
|
|