group lines according to first word

You could do something like:

awk -F: 'NR>1 && $1 "" != last {print ""}; {print; last = $1}'

The "" is to force string comparison. Without it, it wouldn't work properly in input like:

100:foo
100:bar
1e2:baz
1e2:biz

Where 100 and 1e2 would be compared as numbers.


Here's one way. If the first field isn't the same as the one in the previous line, print a break....

$ awk -F: '$1!=a&&a{print ""}{a=$1}1' myfile
cat:persian/young-1
cat:winter/young-2
cat:summer/wild-3

dog:persian/young-1
dog:winter/young-2
dog:summer/wild-3
$

Explanation:

  • -F: = set the field delimiter to :
  • $1!=a&&a = if first field is not equal to variable "a" (the previous first field), and variable "a" is set to something (i.e. we're not dealing with the very first line in the file)
  • {print ""} = print a blank line
  • {a=$1} = for every line read, set variable "a" to the first field
  • 1 = print the line