sort this file using out-of-the-box Redhat Linux tools

Dear script writers,

Is there a way to sort the data in the attached file (using a script) where the oldest data is on the top, and the latest data is at the bottom? It would have to processed, keeping the "blocks" of data together and using the date info from the ".ER" lines.

A ".ER" line is the begining of a block of data. The spacing and format of the data can't be changed. Just need to blocks sorted. The date information is in the "ER" lines, as as follows:
.ER LAWV2    0224 E DC201602241659/DH19/HGIFF/DIH6

Tools must be out of the box Redhad Linux. I know I have python, awk, shell script stuff. Whatever works the best...

Thanks for your help!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Maciej SsysadminCommented:
sed ':L;$!{N;s/\n/|/;tL};s/|\(\.ER\)/\n\1/g' testfile.txt | sort -k5.3 | sed 's/|/\n/g' > sorted.txt

Open in new window

First set of sed commands joins all lines of each block, replacing new line with "|" (I assumed that you can't have any "|" characters in your file). Then it's sorted using fifth (-k5) field (space/tab separated) and starts on third (.3) character of that field (this is place where date is stored). The last step is to revert single lines (sorted) to blocks (replacing each "|" with new line).
Sorted blocks are saved in sorted.txt file.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MarkLoveExExAuthor Commented:
Excellent job. Thank you so much!  One thing I forgot to mention is that sometimes there are 5 lines of data, and sometimes there are 6 lines. But it looks like you took that into account, and the script works perfectly. Thanks again!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.