Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group lines by prefix

Tags:

unix

sed

awk

I have a file full of keys and values which I use for i18n. The file looks like this:

foo.foo=Some string
foo.bar=Some string
bar.foo=Some string
bar.bar=Some string
baz.foo=Some string
baz.bar=Some string

Using some unix tool like awk or sed, how can I filter the file so that lines beginning with the same prefix (up to the first dot) are grouped together, and groups are separated by a blank line?

The output should look like

foo.foo=Some string
foo.bar=Some string

bar.foo=Some string
bar.bar=Some string

baz.foo=Some string
baz.bar=Some string
like image 483
Jezen Thomas Avatar asked Nov 14 '25 21:11

Jezen Thomas


1 Answers

This should do:

awk -F. '$1!=a && NR>1 {print ""} 1; {a=$1}' file
foo.foo=Some string
foo.bar=Some string

bar.foo=Some string
bar.bar=Some string

baz.foo=Some string
baz.bar=Some string
like image 177
Jotne Avatar answered Nov 17 '25 10:11

Jotne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!