How can group entries (in a file by common prefixes)?
As in, one wants to convert:
foo: entry1
bar: entry2
foo: entry3
baz: entry4to
foo: entry1 entry3
bar: entry2
baz: entry4There 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:
{
a[$1] = a[$1] " " $2
}
END{
for (x in a) print x, a[x]
}Written out as a shell command:
awk '{a[$1] = a[$1] " " $2}END{for (x in a) print x, a[x]}' file