Commit ab431c35 authored by User expired's avatar User expired
Browse files

Option -y to use the year as primary sort key

parent a15b0eaa
Pipeline #27548 passed with stage
in 1 minute and 25 seconds
......@@ -13,6 +13,7 @@ Options:
-c check for sorted input only
-i edit bibtex files in place
-r newest entries first (by year)
-y use year as first sort key
EOF
exit $1
}
......@@ -22,6 +23,8 @@ if [ $# -lt 1 ]; then usage 1; fi
CHECK=0 # Only check if sorted.
INPLACE=0 # Edit files in place.
OPTIONS=
OPTONE=
OPTTWO=
# Parse command line options.
for arg; do
......@@ -32,8 +35,11 @@ for arg; do
# With option '-i' edit files in place.
if [[ "$arg" == "-i" ]]; then INPLACE=1; fi
# With option '-r' show newest entries first.
if [[ "$arg" == "-r" ]]; then OPTIONS="$OPTIONS -r"; fi
if [[ "$arg" == "-r" ]]; then OPTONE="-r"; fi
# With option '-y' use year as first sort key.
if [[ "$arg" == "-y" ]]; then OPTTWO="-y"; fi
done
OPTIONS="$OPTONE $OPTTWO"
# If not check only and not writing in place, sort all
......@@ -48,6 +54,7 @@ then
test "$bibfile" != "-c" || continue
test "$bibfile" != "-i" || continue
test "$bibfile" != "-r" || continue
test "$bibfile" != "-y" || continue
# Skip if not a regular file.
test -f "$bibfile" -o "$bibfile" = "-" || {
......@@ -76,6 +83,7 @@ do
test "$bibfile" != "-c" || continue
test "$bibfile" != "-i" || continue
test "$bibfile" != "-r" || continue
test "$bibfile" != "-y" || continue
# Skip if not a regular file.
test -f "$bibfile" -o "$bibfile" = "-" || {
......
......@@ -7,11 +7,12 @@ $(basename $0): Unite bibfiles with sorted keys
Exact duplicates of an entry are silently dropped.
USAGE: $(basename $0) [-e] [-r] bibfiles
USAGE: $(basename $0) [-e] [-r] [-y] bibfiles
Options:
-h print this help
-e do not sort fields in entries
-r newest entries first (by year)
-y use year as first sort key
EOF
exit $1
}
......@@ -39,6 +40,19 @@ if [[ "$1" == "-r" ]]; then
shift
fi
# Default sorting order.
PRIMARY="sort -sf -k1,1" # First sort key.
SECONDARY="sort -sn -k2,2 $RYEAR" # Second sort key.
TERTIARY="sort -sf -k3,3" # Third sort key.
# With option '-y' use year as first sort key.
if [[ "$1" == "-y" ]]; then
TMP="$PRIMARY"
PRIMARY="$SECONDARY"
SECONDARY="$TMP"
shift
fi
# Check for multi line entries and abort if any.
if [ $FIELDSORT -eq 1 ] && grep -H -n '^[^@}% ]' "$@" 1>&2
......@@ -72,9 +86,9 @@ BEGIN {
| sed '/^\x00/!d;/^\x00\x00$/d;/^\x00\x00\x00/d' \
| sed 'h;s/^\x00[^{]*{//;s/,\x00.*//;s/:/ /;s/\([a-z][a-z]*\)\( [^\x00]*\)/\2\1/;s/[0-9][0-9]* / &/;G;s/\n/\x00/' \
| sort -u \
| sort -sf -k3,3 \
| sort -sn -k2,2 $RYEAR \
| sort -sf -k1,1 \
| $TERTIARY \
| $SECONDARY \
| $PRIMARY \
| sed 's/^[^\x00]*\x00\x00/\x00/' \
| gawk '
BEGIN {
......
......@@ -76,12 +76,62 @@ writeFileThree() { # Unsorted bibtex file.
EOF
}
writeFileFour() { # Bibtex file sorted with year as first key.
cat <<EOF
@Article{Olasz2017:cs,
Author = {Olasz, Bal\'{a}zs and Szab\'{o}, Istv\'{a}n and Czak\'{o}, G\'{a}bor},
Journal = {Chem. Sci.},
Year = {2017}
}
@Article{Czako2019b:pccp,
Author = {Czak\'{o}, G\'{a}bor and Olasz, Bal\'{a}zs},
Journal = {Phys. Chem. Chem. Phys.},
Year = {2019},
File = {:by_citekey/subdir/Czako_2019_JPCA.pdf:PDF}
}
@Article{Olasz2019:jpca,
Author = {Olasz, Bal\'{a}zs and Czak\'{o}, G\'{a}bor},
Journal = {J. Phys. Chem. A},
Year = {2019}
}
EOF
}
writeFileFive() { # Bibtex file sorted with year as first key and newest first.
cat <<EOF
@Article{Czako2019b:pccp,
Author = {Czak\'{o}, G\'{a}bor and Olasz, Bal\'{a}zs},
Journal = {Phys. Chem. Chem. Phys.},
Year = {2019},
File = {:by_citekey/subdir/Czako_2019_JPCA.pdf:PDF}
}
@Article{Olasz2019:jpca,
Author = {Olasz, Bal\'{a}zs and Czak\'{o}, G\'{a}bor},
Journal = {J. Phys. Chem. A},
Year = {2019}
}
@Article{Olasz2017:cs,
Author = {Olasz, Bal\'{a}zs and Szab\'{o}, Istv\'{a}n and Czak\'{o}, G\'{a}bor},
Journal = {Chem. Sci.},
Year = {2017}
}
EOF
}
# Initially create temporary files for all tests.
oneTimeSetUp() {
fileOne=$(mktemp -u -t tmp.XXXXXX).bib
fileTwo=$(mktemp -u -t tmp.XXXXXX).bib
fileThree=$(mktemp -u -t tmp.XXXXXX).bib
touch $fileOne $fileTwo $fileThree
fileFour=$(mktemp -u -t tmp.XXXXXX).bib
fileFive=$(mktemp -u -t tmp.XXXXXX).bib
touch $fileOne $fileTwo $fileThree $fileFour $fileFive
}
# Finally remove temporary files for all tests.
......@@ -89,6 +139,8 @@ oneTimeTearDown() {
test ! -e $fileOne || rm $fileOne
test ! -e $fileTwo || rm $fileTwo
test ! -e $fileThree || rm $fileThree
test ! -e $fileFour || rm $fileFour
test ! -e $fileFive || rm $fileFive
}
# For each test reset the temporary file contents.
......@@ -96,6 +148,8 @@ setUp() {
writeFileOne > $fileOne
writeFileTwo > $fileTwo
writeFileThree > $fileThree
writeFileFour > $fileFour
writeFileFive > $fileFive
}
# _____________________________________________________________________________
......@@ -123,6 +177,11 @@ testSortEntries() {
assertTrue 'Make default order' "bib-order $fileTwo | diff $fileOne -"
assertTrue 'Make newest first' "bib-order -r $fileOne | diff $fileTwo -"
assertTrue 'Keep newest first' "bib-order -r $fileTwo | diff $fileTwo -"
assertTrue 'Make year first key (newest last)' "bib-order -y $fileThree | diff $fileFour -"
assertTrue 'Keep year first key (newest last)' "bib-order -y $fileFour | diff $fileFour -"
assertTrue 'Make year first key (newest first)' "bib-order -r -y $fileThree | diff $fileFive -"
assertTrue 'Keep year first key (newest first)' "bib-order -r -y $fileFive | diff $fileFive -"
assertFalse 'Assure that tests could fail' "bib-order -r -y $fileFive | diff $fileFour -"
}
testSortInPlaceDefault() {
......
......@@ -68,23 +68,77 @@ writeFileThree() { # Bibtex file with meta information by JabRef.
EOF
}
writeFileFour() { # Bibtex file sorted with year as first key.
cat <<EOF
@Article{Olasz2017:cs,
Author = {Olasz, Bal\'{a}zs and Szab\'{o}, Istv\'{a}n and Czak\'{o}, G\'{a}bor},
Journal = {Chem. Sci.},
Year = {2017}
}
@Article{Czako2019b:pccp,
Author = {Czak\'{o}, G\'{a}bor and Olasz, Bal\'{a}zs},
Journal = {Phys. Chem. Chem. Phys.},
Year = {2019},
File = {:by_citekey/subdir/Czako_2019_JPCA.pdf:PDF}
}
@Article{Olasz2019:jpca,
Author = {Olasz, Bal\'{a}zs and Czak\'{o}, G\'{a}bor},
Journal = {J. Phys. Chem. A},
Year = {2019}
}
EOF
}
writeFileFive() { # Bibtex file sorted with year as first key and newest first.
cat <<EOF
@Article{Czako2019b:pccp,
Author = {Czak\'{o}, G\'{a}bor and Olasz, Bal\'{a}zs},
Journal = {Phys. Chem. Chem. Phys.},
Year = {2019},
File = {:by_citekey/subdir/Czako_2019_JPCA.pdf:PDF}
}
@Article{Olasz2019:jpca,
Author = {Olasz, Bal\'{a}zs and Czak\'{o}, G\'{a}bor},
Journal = {J. Phys. Chem. A},
Year = {2019}
}
@Article{Olasz2017:cs,
Author = {Olasz, Bal\'{a}zs and Szab\'{o}, Istv\'{a}n and Czak\'{o}, G\'{a}bor},
Journal = {Chem. Sci.},
Year = {2017}
}
EOF
}
# Initially create temporary files for all tests.
oneTimeSetUp() {
fileOne=$(mktemp -u -t tmp.XXXXXX).bib
fileTwo=$(mktemp -u -t tmp.XXXXXX).bib
touch $fileOne $fileTwo
fileFour=$(mktemp -u -t tmp.XXXXXX).bib
fileFive=$(mktemp -u -t tmp.XXXXXX).bib
touch $fileOne $fileTwo $fileFour $fileFive
}
# Finally remove temporary files for all tests.
oneTimeTearDown() {
test ! -e $fileOne || rm $fileOne
test ! -e $fileTwo || rm $fileTwo
test ! -e $fileFour || rm $fileFour
test ! -e $fileFive || rm $fileFive
}
# For each test reset the temporary file contents.
setUp() {
writeFileOne > $fileOne
writeFileTwo > $fileTwo
writeFileFour > $fileFour
writeFileFive > $fileFive
test ! -d by_citekey || rm -rf by_citekey
bib-separate $fileOne >/dev/null
}
......@@ -115,6 +169,12 @@ testUnitingNewEntriesFirst() {
"bib-unite by_citekey/*.bib by_citekey/*/*.bib | diff $fileTwo -"
assertTrue 'United file with newest entries first' \
"bib-unite -r by_citekey/*.bib by_citekey/*/*.bib | diff $fileTwo -"
assertTrue 'United file with year as first sort key' \
"bib-unite -y by_citekey/*.bib by_citekey/*/*.bib | diff $fileFour -"
assertTrue 'United file with latest year first' \
"bib-unite -r -y by_citekey/*.bib by_citekey/*/*.bib | diff $fileFive -"
assertFalse 'Assure that tests could fail' \
"bib-unite -r -y by_citekey/*.bib by_citekey/*/*.bib | diff $fileFour -"
}
testDropDuplicates() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment