I have a file looks like:
id001 success:100
id002 success:99
id002 failed:1
id003 failed:100
id004 success:50
id004 failed:50
I want to format it as:
id001 succe开发者_Python百科ss:100
id002 success:99 failed:1
id003 failed:100
id004 success:50 failed:50
Could someone give me a solution? Thanks!
awk '{a[$1]=a[$1] FS $2}END{for(i in a) print i,a[i]}' file
Alternative Ruby(1.9+)
$ ruby -ane 'BEGIN{a=Hash.new};a[$F[0]]="#{a[$F[0]]} #{$F[1]}"; END{ a.each{|x,y| puts "#{x}#{y}" }}' file
Using 'awk':
awk '{line[$1] = line[$1] " " $2}
END { for (id in line) { printf "%s%s\n", id, line[id] } }'
It's short (but cryptic) in sed:
sed '$!N;/^\([^ ]*\) .*\n\1/{s/\n[^ ]* / /;};P;D' file
Without using awk
(just for the fun of it):
for i in `grep -o "id[0-9]*" myfile`; do
echo -n "$i "
grep $i myfile | cut -c 7- |xargs
done | uniq
Didn't see any Perl answers. So here you go:
perl -lnae '$h{$F[0]}.="$F[1] ";END{print "$_ $h{$_}"for(sort keys %h)}' file
See it on Ideone
精彩评论