1

There are plenty of tools that will sort a text file on the assumption that the unit of interest is the line, but are there any that work on blocks?

Example:

name = Mercury order = 1 mass = 0.06 name = Venus order = 2 mass = 0.82 name = Earth order = 3 mass = 1 

Is there a tool that would sort the list of planets optionally by name, mass or order from the sun, taking as input and producing as output a text file with each record being a block like the above?

1 Answer 1

2

For files that fit into memory you could use Perl, you can define the input record separator.

 perl -e '$/="\n\n"; print sort <>' t.txt 

If the file is too big for memory but you have an equal amount of free disk space, you can do a three step sort:

  • use perl to merge lines of each record
  • sort
  • use perl to split lines of each record

To sort by the value of one of the attributes, I would read the data into a suitable data structure such as an array of hashes AoH and define a comparator as described in perldoc -f sort

Alternatively I would

  • Use perl to convert to CSV of values only
  • sort
  • Use perl to convert CSV back to multiline records with name=value pairs

It would probably be best to do the whole job in Perl.

Other programming languages are available.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.