Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort a file broken into sections by (DOW MON DD YY) date in each section's header

I have a file that has repetitive entries .Entry entry starts with date , a blank line separates these two entries .How do I use sort command or uniq comand to sort the dates :

* Mon Jan 29 2001 Bernhard Rosenkraenzer <[email protected]>
- Some fixes to init scripts

* Wed Jan 17 2001 Bernhard Rosenkraenzer <[email protected]>
- Add missing man pages, fix up init script (Bug #17676)
- Can be patched in for future release.


* Thu Feb 1 2001 Trond Eivind Glomsrød <[email protected]>
- Fix check for ipchains

I used the sort command like:

sort -n -t" " -k5 -k3M -k4

but it does not captures the lines after the date , I need to sort the date+contentforthatdate.

like image 650
Unixquest945 Avatar asked Jan 25 '26 11:01

Unixquest945


1 Answers

Based on @jchevali's comment - one just needs to find a printable character that both awk and tr can handle gracefully that's not in the text to be processed:

awk '/^*/{printf "%s ^",$0} /^-/{printf "%s ^",$0} /^[ \t]*$/{printf "\n"}' paras | sort -n -t" " -k5 -k3M -k4  | tr '^' '\n'

* Wed Jan 17 2001 Bernhard Rosenkraenzer <[email protected]> 
- Add missing man pages, fix up init script (Bug #17676) 
- Can be patched in for future release. 

* Mon Jan 29 2001 Bernhard Rosenkraenzer <[email protected]> 
- Some fixes to init scripts 

* Thu Feb 1 2001 Trond Eivind Glomsrød <[email protected]> 
- Fix check for ipchains
like image 155
tink Avatar answered Jan 29 '26 01:01

tink



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!