LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Combining records with same key (https://www.linuxquestions.org/questions/linux-newbie-8/combining-records-with-same-key-800031/)

danielbmartin 04-04-2010 09:20 PM

Combining records with same key
 
Hello.

I'm new to Linux and struggling to learn sed on my own. Therefore a sed solution is most desirable, but all advice will be gratefully accepted.

Input file (already sorted on the key field):
204 Daniel Barton
206 Mary Halston
206 David Coleman
206 Carson Coleman
207 Roland Highsmith
207 Annie Highsmith
209 Connie Ward

Desired output file:
204 Daniel Barton
206 Mary Halston David Coleman Carson Coleman
207 Roland Highsmith Annie Highsmith
209 Connie Ward

How may this be achieved? Thanks in advance.

Daniel B. Martin

grail 04-04-2010 11:11 PM

Not sure in sed (and not sure the right tool), but this works:

Code:

awk '{ arr[$1] = arr[$1]" "$2" "$3 }END{ for (x in arr) print x arr[x]}' file


All times are GMT -5. The time now is 05:28 PM.