How to group a list by name with a newline after each group in shell? -
i have list group name. should done newline after each group. here example file:
$ cat file 2015-07-09 07:03:46 7.5 gib apple-foo.txt.gpg 2015-07-22 11:36:36 6.9 gib apple-bar.txt.gpg 2015-07-27 04:40:34 31.0 gib banana-here.txt.gpg 2015-07-07 20:28:17 30.6 gib banana-even.txt.gpg 2015-07-19 15:02:20 30.8 gib banana-more.txt.gpg 2015-07-26 00:05:11 1.9 gib coconut-something.txt.gpg 2015-07-23 03:34:41 2.1 gib coconut-else.txt.gpg 2015-07-24 03:34:40 12.1 gib date-yougetit.txt.gpg
and output trying get:
2015-07-09 07:03:46 7.5 gib apple-foo.txt.gpg 2015-07-22 11:36:36 6.9 gib apple-bar.txt.gpg 2015-07-27 04:40:34 31.0 gib banana-here.txt.gpg 2015-07-07 20:28:17 30.6 gib banana-even.txt.gpg 2015-07-19 15:02:20 30.8 gib banana-more.txt.gpg 2015-07-26 00:05:11 1.9 gib coconut-something.txt.gpg 2015-07-23 03:34:41 2.1 gib coconut-else.txt.gpg 2015-07-24 03:34:40 12.1 gib date-yougetit.txt.gpg
i manage extract unique names (apple, banana, coconut, date) failing add new line after last occurrence of each unique name. able me out? awk , sed welcome.
an awk
solution:
awk -f\- 'nr>1&&$1!=last{print ""}{last=$1}1' infile
explanation
-f\-
:set field separator –
.
nr>1
:omit first line checking.
last=$1
:always save last occurrence of group key.
1
:print current line.
1!=last{print ""}
:if key $1
not equal last
print separator.
update
for current source use:
awk 'split($nf,a,"-"){current=a[1]}nr>1&¤t!=last{print ""}{last=current}1' infile
explanation 2
split($nf,a,"-"){current=a[1]
:to key a[1]
last field of line $nf
splitting @ -
char.
Comments
Post a Comment