Combine lines with matching first field

后端 未结 4 2031
小蘑菇
小蘑菇 2021-01-18 12:46

For a few years, I often have a need to combine lines of (sorted) text with a matching first field, and I never found an elegant (i.e. one-liner unix command line) way to do

4条回答
  •  旧巷少年郎
    2021-01-18 13:30

    for F in `cut -f1 -d ':' infile.txt | sort | uniq`; do echo "$F:$(grep $F infile.txt | cut -f2- -d ':' | paste -s -d ';' - )"; done
    

    Not sure it qualifies as 'elegant', but it works, though I'm sure not quickly for millions of lines - as the number of grep calls increases it would slow significantly. What % of the matching fields do you expect to be unique?

提交回复
热议问题