[GLLUG] Merging Records in Input

JLMS jjllmmss at googlemail.com
Mon Dec 9 15:05:35 UTC 2013


sort -n my_file | awk 'BEGIN{previous=""} $1 != previous { printf("\n%s
%s",$1,$2);previous=$1;next} $1 == previous {printf(" %s",$2)} END{print
""}'

where my_file has your records.

If the records are sorted already:

awk 'BEGIN{previous=""} $1 != previous { printf("\n%s
%s",$1,$2);previous=$1;next} $1 == previous {printf(" %s",$2)} END{print
""}'  my_file





On 5 December 2013 22:38, Mick Farmer <mick at plan7.co.uk> wrote:

> Dear GLLUGers,
>
> I have an input consisting of two fields such as
>
> ...
> 7 010c
> 7 060b
> 8 003d
> 8 003g
> 8 005n
> 8 008c
> 8 010a
> 9 100c
> 11 005m
> 11 013a
> ...
>
> which I want to merge on the first field (key) as follows
>
> ...
> 7 010c 060b
> 8 003d 003g 005n 008c 010a
> 9 100c
> 11 005m 013m
> ...
>
> I'm sure there's a UNIX utility that does this (sort?, uniq?) but can't
> currently find it.  Can anyone help?
>
> Many thanks in advance.
>
> Regards,
>
> Mick
> --
> Dr Mick Farmer
>
> Registered Linux User# 551012
>
>
>
> _______________________________________________
> GLLUG mailing list
> GLLUG at mailman.lug.org.uk
> https://mailman.lug.org.uk/mailman/listinfo/gllug
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.lug.org.uk/pipermail/gllug/attachments/20131209/654aba6c/attachment.html>


More information about the GLLUG mailing list