Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.