Issue
I've a file with following lines:
Name: Karl
Born: 2003-07-06 17:21:36.964
State: Germany
Hobby: Videogames
Name: Albert
Born: 2003-07-05 18:16:19.369
State: UK
Hobby: Soccer
Name: Mark
Born: 2003-06-15 16:58:21.201
State: Italy
Hobby: Jogging
I need a shell script that orders from the older to the younger the lines between the blank lines based on the Born field. Based on the example given the right file result should be:
Name: Mark
Born: 2003-06-15 16:58:21.201
State: Italy
Hobby: Jogging
Name: Albert
Born: 2003-07-05 18:16:19.369
State: UK
Hobby: Soccer
Name: Karl
Born: 2003-07-06 17:21:36.964
State: Germany
Hobby: Videogames
I'm unable to do such thing, please no perl or python... I need bash. Ty all in advance for your help. Regards, Dave
Solution
Since you alread have some answers, may as well throw out another one... using GNU awk for PROCINFO["sorted_in"]
:
$ awk -v RS= -v ORS='\n\n' -F'\n' '
{ recs[$2] = ( $2 in recs ? recs[$2] ORS : "" ) $0 }
END {
PROCINFO["sorted_in"] = "@ind_str_asc"
for (i in recs) {
print recs[i]
}
}
' file
Name: Mark
Born: 2003-06-15 16:58:21.201
State: Italy
Hobby: Jogging
Name: Albert
Born: 2003-07-05 18:16:19.369
State: UK
Hobby: Soccer
Name: Karl
Born: 2003-07-06 17:21:36.964
State: Germany
Hobby: Videogames
See Multiple-Line Records for what those RS
, ORS
, and FS
(-F
) settings do.
The above reads all of your input into memory before printing it and preserves the relative input order of records that have the same timestamp as each other.
Answered By - Ed Morton Answer Checked By - Willingham (WPSolving Volunteer)