Sorting a file of book records


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting a file of book records
# 1  
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.
Code:
@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.



Code:
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,,


Last edited by rbatte1; 12-05-2019 at 10:36 AM.. Reason: Tidied away needless newlines and added ICODE tag.
# 2  
Like so?
Code:
awk '$1=$1' RS= FS="\n" OFS=\| file | sort -t"|" -k3,3 | awk '$1=$1' FS="|" OFS="\n" ORS="\n\n"

This User Gave Thanks to RudiC For This Post:
# 3  
I have included sort -r -t"|" -k3,3 to have records with recent years to show first.
# 4  
Sorting by Year, then sorting by Author Name

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.

Last edited by Danette; 12-05-2019 at 09:11 AM..
# 5  
Try adding a field separator after the publishing year:
Code:
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.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #136
Difficulty: Easy
The IEEE named Linus Torvalds as the recipient of the IEEE Computer Society's Computer Pioneer Award in 2014.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting group of records and loading last record

Hi Everyone, I have below record set. File is fixed widht file 101newjersyus 20150110 101nboston us 20150103 102boston us 20140106 102boston us 20140103 I need to group record based on first 3 letters in our case(101 and 102) and sort last 8 digit in ascending order and print only... (4 Replies)
Discussion started by: patricjemmy6
4 Replies

2. Shell Programming and Scripting

Separate records of a file on 2 types of records

Hi I am new to shell programming in unix Please if I can provide help. I have a file structure of a header record and "N" detail records. The header record will be the total number of detail records I need to split the file in 2: One for the header Another for all detail records Could... (1 Reply)
Discussion started by: jamcogar
1 Replies

3. Shell Programming and Scripting

Deleting duplicate records from file 1 if records from file 2 match

I have 2 files "File 1" is delimited by ";" and "File 2" is delimited by "|". File 1 below (3 record shown): Doc1;03/01/2012;New York;6 Main Street;Mr. Smith 1;Mr. Jones Doc2;03/01/2012;Syracuse;876 Broadway;John Davis;Barbara Lull Doc3;03/01/2012;Buffalo;779 Old Windy Road;Charles... (2 Replies)
Discussion started by: vestport
2 Replies

4. UNIX for Dummies Questions & Answers

Grep specific records from a file of records that are separated by an empty line

Hi everyone. I am a newbie to Linux stuff. I have this kind of problem which couldn't solve alone. I have a text file with records separated by empty lines like this: ID: 20 Name: X Age: 19 ID: 21 Name: Z ID: 22 Email: xxx@yahoo.com Name: Y Age: 19 I want to grep records that... (4 Replies)
Discussion started by: Atrisa
4 Replies

5. Shell Programming and Scripting

Sorting the records for a file

hi, Please suggest me how to do this logic say i am dynamically taking a file name into the script. when ever a file name it should sort the all the records based on the first character in every line except the heading line and ending line. for example file1 heading... (1 Reply)
Discussion started by: angel12345
1 Replies

6. Shell Programming and Scripting

Sorting the records in the Flat file

Hi all, I am using this command "sort -d -u -k1 IMSTEST.74E -o tmp.txt" to the records in the flat. Can any tell me how to sort the file except first line in the file For ex: i/p First line: DXYZ Second line : jumy third : cmhk fourth : andy Output should... (5 Replies)
Discussion started by: sudhir_barker
5 Replies

7. Shell Programming and Scripting

Based on num of records in file1 need to check records in file2 to set some condns

Hi All, I have two files say file1 and file2. I want to check the number of records in file1 and if its atleast 2 (i.e., 2 or greater than 2 ) then I have to check records in file2 .If records in file2 is atleast 1 (i.e. if its not empty ) i have to set some conditions . Could you pls... (3 Replies)
Discussion started by: mavesum
3 Replies

8. UNIX for Advanced & Expert Users

Records mismatch while sorting in MPRAS and SunOS

Hi, As part of migrating a script from SunOS to MP RAS, the following script is not functioning in desired way. sort -t\| -u +6 -7 +12 -14 textfile1.out > \ textfile2uniq.out The -u is fetching unique records differently on SunOS and MP RAS Pentium IV. And thus the records... (1 Reply)
Discussion started by: RRVARMA
1 Replies

9. UNIX for Dummies Questions & Answers

Use records from one file to delete records in another file

file_in_1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 file_in_2: 9 10 11 12 21 22 23 24 1 2 3 4 17 18 19 20 file_out: (5 Replies)
Discussion started by: kenneth.mcbride
5 Replies

10. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies