Introducing a line break into a file having different columns on the basis of text in the value column

$ awk 'NR > 1 && $1 != prev { print "" } { prev = $1 }; 1' pdb_ligands
1aa6 HETATM 4MO A 803
1aa6 HETATM SF4 A 800

1ao0 HETATM 5GP A 467
1ao0 HETATM SF4 B 466
1ao0 HETATM SF4 C 466

1b0y HETATM SF4 A  87

1blu HETATM SF4 A 101
1blu HETATM SF4 A 102

This keeps track of what was in the 1st column on the previous line in prev.

If the current 1st column is different from prev, and we are not on the first line of the file, a newline is printed. All lines are then printed unconditionally.


An alternative to print "" in the code above is to do $0 = ORS $0, which adds a newline character (or whatever ORS, the output record separator, is set to) to the start of the current record.

This will have the effect of producing an extra newline when the line is printed moments later.