[GLLUG] Merging Records in Input

Paul Hewlett phewlett76 at gmail.com
Thu Dec 5 23:11:28 UTC 2013


A python snippet:

#/usr/bin/env python

import sys
from collections import defaultdict

d = defaultdict(list)

with open(sys.argv[1], 'rtU') as fd:
    for i in iter(fd):
        k, v = i.split()
        d[k].append(v)

for k, v in d.iteritems():
    sys.stdout.write(k)
    for x in v:
        sys.stdout.write(' ')
        sys.stdout.write(x)
    sys.stdout.write('\n')

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



-- 
Tel: +44 79 8532 7353
LinkedIn: http://uk.linkedin.com/pub/paul-hewlett/0/629/9b4




More information about the GLLUG mailing list