Note: the complementary question is here: How to separately sort lines within multiple “chunks” separated with headers?
So what I did find an answer to, is how to sort lines in a text files alphabetically.
But, it is not exactly what I need to do.
I have this file with profiles containing 15 different parameters that goes into an instrument at work and is read by the machine to have a list of profiles.
Sadly, the formatting of this file looks like this:
[ProfileB]
param1=z
param2=y
param3=x
[ProfileA]
param1=k
param2=l
param3=
And I want to sort the Profiles alphabetically, but I need them to stay grouped with their parameters. The above example should be sorted like this:
[ProfileA]
param1=k
param2=l
param3=
[ProfileB]
param1=z
param2=y
param3=x
I guess there is something to work either with the fixed number of lines (name+parameters) or with the character "[" as an identifier for the beginning of a group of lines.
But this is beyond my capacity in text manipulation.
I have at my disposition either Sublime Text, R, or Linux command console.
Best Answer
This works in my Debian:
To work with file(s) use redirection(s), e.g.
{ sed … ; } <input.txt >output.txt
, wheresed …
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.