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

MarkLoveExEx
MarkLoveExEx used Ask the Experts™
on
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
YYYYMMDDhhmm

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!
testfile.txt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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.

Author

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!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial