Sorting a file of book records

I have a texinfo file containing book records and I want to sort them. An example is shown below.
Records are separated by two blank lines. The sort pattern I want to sort is starting from the beginning of the year declaration and finishing at the beginning of the book title where I use the command @strong .

The resulting file would look the same as the original, but the records will be sorted.

@sp 1
@anchor{bibl--Borcherdt--1982}
1982.@: @sc{Borcherdt, Roger D.}; @strong{Reflection-refraction of
general P- and Type-I S-waves in elastic and anelastic solids.}
Geophysical Journal of the Royal Astronomical Society, Volume 70, Pages
621-638.
@uref{academic.oup.com/gji/article/70/3/621/623392,,
[@sc{Complete Article}]} [@code{bibl--Borcherdt--1982}].
@c Published: September 01, 1982.


@sp 1
@anchor{bibl--Kennett--2007}
2007.@: @sc{Kennett, Brian}; @strong{Seismic phases.}  In: Gubbins,
David; Herrero-Bervera, Emilio (Eds.); "Encyclopedia of Geomagnetism and
Paleomagnetism."
@uref{link.springer.com/referenceworkentry/10.1007/978-1-4020-4423-6_290,,
[@sc{Doi}]} [@code{bibl--Kennett--2007}].
@c Published: 


@sp 1
@anchor{bibl--Bormann--et--Storchak--et--Schweitzer--2013}
2013.@: @sc{Bormann, Peter}; Storchak, Dmitry A.; Schweitzer, Johannes;
@strong{The IASPEI standard nomenclature of seismic phases.}  In:
Bormann, P.@: (Ed.), "New Manual of Seismological Observatory Practice 2
(NMSOP-2)".
@uref{gfzpublic.gfz-potsdam.de/pubman/faces/viewItemOverviewPage.jsp?itemId=escidoc:152435:3#files,,
[@sc{Complete Article}]} [@code{bibl--Bormann--2013}].
@c Published: September 01, 2013.

 

Have tried using awk with a space for RS to distinguish each paragraph. However

the result is all messed up.

awk -v RS="" -v cmd=sort '{print | cmd; close(cmd); print ""}' Ch02c--Rfc.texi

1959.@: @sc{Brune, J.N.}; Oliver, J.; @strong{The seismic noise of the
@anchor{bibl--Brune--et--Oliver--1959}
@c Published: October 01, 1959.
Earth's surface.}  Bulletin of the Seismological Society of America,
[@sc{Doi}]} [@code{bibl--Brune--et--Oliver--1959}].
@sp 1
@uref{pubs.geoscienceworld.org/ssa/bssa/article-abstract/49/4/349/115922/the-seismic-noise-of-the-earth-s-surface,,
Volume 49, Pages 349-353.

1959.@: @sc{Knopoff, Leon}, @strong{Scattering of compression waves by
@anchor{bibl--Knopoff--1959}
@c Published 1959.
[@sc{Doi}]} [@code{bibl--Knopoff--1959}].
@sp 1
spherical obstacles.} Geophysics, Volume 24, Pages 30-39.
@uref{pubs.geoscienceworld.org/geophysics/article-abstract/24/1/30/67439/scattering-of-compression-waves-by-spherical,,

1950.@: @strong{Longuet-Higgins, Michael Selwyn}; @strong{A Theory of
243, No.@: 857, Pagination 1-35.
@anchor{bibl--Longuet--Higgins--1950}
@c Published: September 27, 1950.
[@sc{Complete Article}]} [@code{bibl--Longuet--Higgins--1950}].
Society of London, Series A, Mathematical and Physical Sciences, Volume
@sp 1
the Origin of Microseisms.}  Philosophical Transactions of the Royal
@uref{epsc.wustl.edu/~ggeuler/reading/cam_noise_biblio/longuet-higgins_1950-ptrsl-a_theory_of_the_origin_of_microseisms.pdf,,

Like so?

awk '$1=$1' RS= FS="\n" OFS=\| file | sort -t"|" -k3,3 | awk '$1=$1' FS="|" OFS="\n" ORS="\n\n"
1 Like

I have included sort -r -t"|" -k3,3 to have records with recent years to show first.

Almost everything is going well. However I would like to elaborate a bit more. After I sort in reverse order
I end up with the book listed sorted by year, with most recent on top.

But for records of the same year, I have the author names sorted in reverse as well.

Wnat I would like to have is to sort by year so that the most recent are shown first. But for the same year,
things are sorted by author name alphabetically in order A-Z.

I would not mind adding some form of tag to each record to do this if the command would then be simpler.
As I am using texinfo and build my document using makeinfo, it does not create any problems if I also
add some additional tags to each record, for example on the third line.

Try adding a field separator after the publishing year:

awk '{sub (/[0-9][0-9][0-9][0-9]\.@: /, "&|"); $1=$1} 1' RS= FS="\n" OFS=\| file | sort -t"|" -k3,3r -k4,4 | awk '$1=$1' FS="|" OFS="\n" ORS="\n\n"

If there is a simpler pattern (e.g. the @sc{ ) to match in every record, you can try that, e.g.: sub (/@sc{/, "|&") . If you dislike the additional line feed in the output, do the reverse in the second awk .