Sort packs of lines alphabetically
Solution 1:
This works in my Debian:
sed '1 ! s/^\[/\x00\[/g' | sort -z | tr -d "\0"
To work with file(s) use redirection(s), e.g. { sed … ; } <input.txt >output.txt
, where sed …
is the whole command.
The procedure is as follows:
-
sed
inserts a null character before every[
that is in the beginning of a line, unless the line is the first one. This way null characters separate profiles. -
sort -z
uses these null characters as separators, so it sorts whole profiles, not separate lines. -
tr
deletes null characters.