Hi all,
I So, I've got a monster text document comprising a list of various company names and associated info just in a long list one after another. I need to sort them alphabetically by name...
The text document looks like this:
Company Name:
the_first_company's_name_here
Address:
an_address_here
Phone:
phone_number_here
SomeOtherValue:
the_value_here
Name:
the_next_name_here
Address:
the_second_company's_address_here
Phone:
phone_number_here
SomeOtherValue:
another_value_here
etc.
If I only wanted to alphabetically sort the names then that would be a simple:
sed -n '/Company Name/{n;p;}' | sort
but that doesn't help me rearrange all the other bits of data associated with each Company Name. My grasp of regular expressions is woefully poor... is there a simple way to chop up the file into separate records by using the line "Company Name:" to signal the start of each new record, alphabetically sort the values for each Company Name: field, and then reassemble the document with all the other bits of each record in the right place?
The value for each field is always on the line immediately below it. Each record is of varying length, as some lack certain fields (e.g., not all will have a SomeOtherValue: field, or whatever). Some have data across multiple lines. The only things that are common throughout are the Company Name: field which is ALWAYS at the start of a new record, and the fact that there are always 3 empty lines immediately preceding each "Company Name:" label as in the example above.
Does anybody have an elegant solution for this? Or a not-so-elegant solution?
Thanks!
