Size: 1160
Comment: converted to 1.6 markup
|
← Revision 6 as of 2012-03-29 20:36:56 ⇥
Size: 1389
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 2: | Line 2: |
== How can group entries (in a file by common prefixes)? == | == How can I group entries (in a file by common prefixes)? == |
Line 4: | Line 4: |
Line 10: | Line 11: |
Line 11: | Line 13: |
Line 36: | Line 39: |
And a basic implementation of '''b''' in awk: | And a basic implementation of '''b''' in awk, using a true multi-dimensional array: |
Line 39: | Line 42: |
a[$1] = a[$1] " " $2 | a[$1,++b[$1]] = $2; |
Line 41: | Line 44: |
END{ for (x in a) print x, a[x] |
END { for (i in b) { printf("%s", i); for (j=1; j<=b[i]; j++) { printf(" %s", a[i,j]); } print ""; } |
Line 44: | Line 54: |
Line 45: | Line 56: |
Line 47: | Line 59: |
awk '{a[$1] = a[$1] " " $2}END{for (x in a) print x, a[x]}' file | awk '{a[$1,++b[$1]]=$2} END {for (i in b) {printf("%s", i); for (j=1; j<=b[i]; j++) printf(" %s", a[i,j]); print ""}}' file |
How can I group entries (in a file by common prefixes)?
As in, one wants to convert:
foo: entry1 bar: entry2 foo: entry3 baz: entry4
to
foo: entry1 entry3 bar: entry2 baz: entry4
There are two simple general methods for this:
- sort the file, and then iterate over it, collecting entries until the prefix changes, and then print the collected entries with the previous prefix
- iterate over the file, collect entries for each prefix in an array indexed by the prefix
A basic implementation of a in bash:
old=xxx ; stuff= (sort file ; echo xxx) | while read prefix line ; do if [[ $prefix = $old ]] ; then stuff="$stuff $line" else echo "$old: $stuff" old="$prefix" stuff= fi done
And a basic implementation of b in awk, using a true multi-dimensional array:
{ a[$1,++b[$1]] = $2; } END { for (i in b) { printf("%s", i); for (j=1; j<=b[i]; j++) { printf(" %s", a[i,j]); } print ""; } }
Written out as a shell command:
awk '{a[$1,++b[$1]]=$2} END {for (i in b) {printf("%s", i); for (j=1; j<=b[i]; j++) printf(" %s", a[i,j]); print ""}}' file