bib-unite 9.71 KB
Newer Older
1
2
3
4
#!/bin/bash
# Unite bibfiles with sorted keys

function usage() {
5
6
    echo "$(basename $0): Unite bibfiles with sorted keys"
    echo ""
7
8
    echo "Exact duplicates of an entry are silently dropped."
    echo ""
9
    echo "USAGE: $(basename $0) [-e] [-r] bibfiles"
10
    echo "Options:"
User expired's avatar
User expired committed
11
    echo "  -h  print this help"
12
    echo "  -e  do not sort fields in entries"
13
14
15
16
17
18
19
    echo "  -r  newest entries first (by year)"
    exit $1
}
if [ $# -lt 1 ]; then usage 1; fi

# Default options.
RYEAR= # Show newest entries last.
20
FIELDSORT=1 # Sort fields within entries.
21
22
23
24
25
26
27

# Parse command line options.
for arg; do
    # With option '-h' print usage and exit.
    if [[ "$arg" == "-h" ]]; then usage 0; fi
done

28
29
30
31
32
33
# With option '-e' do not sort fields.
if [[ "$1" == "-e" ]]; then
    FIELDSORT=0
    shift
fi

34
35
36
37
38
39
40
# With option '-r' show newest entries first.
if [[ "$1" == "-r" ]]; then
    RYEAR=-r
    shift
fi


41
42
43
44
45
46
47
48
49
50
# Check for multi line entries and abort if any.
if [ $FIELDSORT -eq 1 ] && grep -H -n '^[^@}% ]' "$@" 1>&2
then
    echo "" 1>&2
    echo "bib-unite: There are multi line fields! Aborting." 1>&2
    echo "bib-unite: Please remove the newlines (see above)." 1>&2
    exit 1
fi


51
52
# Concatenate and sort all files.
sed '1s/^/\n\n/;$s/$/\n@/;N;s/\([^\n]\)\n@/\1\n\n@/;P;D' "$@" \
53
    | gawk '
54
55
56
57
BEGIN {
    RS="\n\n@"
    ORS="\n"
    FS="\n"
58
    OFS="\0" # Requires GNU AWK.
59
60
}
{
61
    # Print one line records with fields delimited by NUL.
62
63
64
65
66
67
68
    for(i=1; i<=NF; i++) {
        # Add empty field in the beginning.
        printf("%s%s", OFS, $i)
    }
    printf(ORS)
}
' \
69
    | sed '/^\x00Comment{jabref-meta/d' \
70
71
    | 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/' \
72
    | sort -u \
73
74
75
    | sort -sf -k3,3 \
    | sort -sn -k2,2 $RYEAR \
    | sort -sf -k1,1 \
76
    | sed 's/^[^\x00]*\x00\x00/\x00/' \
77
    | gawk '
78
79
80
BEGIN {
    RS="\n"
    ORS="\n"
81
    FS="\0" # Requires GNU AWK.
82
83
84
85
86
87
88
89
90
91
92
93
    OFS="\n"
}
{
    # Print sorted records with initial newlines.
    printf("@")
    for (i=2; i<=NF; i++) {
        # Omit empty field added above.
        printf("%s%s", $i, OFS)
    }
    printf(ORS)
}
' \
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
    | gawk -v fieldsort=$FIELDSORT '
BEGIN {
    RS="\n\n@"
    ORS=RS
    FS="\n"
    OFS=FS
}
{
    # Split citation entry into fields.
    split($0, a)

    # Entry type without preceding @ in first entry.
    type = $1
    sub("@", "", type)

    # Sort fields if desired.
    if (fieldsort) {
        loopshift=0 # @ will be on previous before last line.
        # Rename fields to mostly sort like JabRef.
        for(i=1; i<=NF; i++) {
            if (match(type, "^Misc")) {
                gsub("^  Author", "  01Author", a[i])
                gsub("^  Title", "  02Title", a[i])
                gsub("^  Howpublished", "  03Howpublished", a[i])
                gsub("^  Month", "  04Month", a[i])
                gsub("^  Year", "  05Year", a[i])
                gsub("^  Note", "  06Note", a[i])
            } else if (match(type, "^[^}]*thesis")) {
                gsub("^  Author", "  01Author", a[i])
                gsub("^  Title", "  02Title", a[i])
                gsub("^  School", "  03School", a[i])
                gsub("^  Year", "  04Year", a[i])
                gsub("^  Type", "  05Type", a[i])
                gsub("^  Month", "  06Month", a[i])
            } else if (match(type, "^In[cp]")) {
                gsub("^  Author", "  01Author", a[i])
                gsub("^  Title", "  02Title", a[i])
                gsub("^  Booktitle", "  03Booktitle", a[i])
                if (match(type, "^Incollection")) {
                    gsub("^  Publisher", "  03Publisher", a[i])
                }
                gsub("^  Year", "  04Year", a[i])
                gsub("^  Editor", "  05Editor", a[i])
                gsub("^  Volume", "  06Volume", a[i])
                gsub("^  Number", "  07Number", a[i])
                gsub("^  Series", "  08Series", a[i])
                gsub("^  Pages", "  09Pages", a[i])
                gsub("^  Address", "  10Address", a[i])
                gsub("^  Month", "  11Month", a[i])
                gsub("^  Organization", "  12Organization", a[i])
                gsub("^  Publisher", "  13Publisher", a[i])
                gsub("^  Note", "  14Note", a[i])
            } else if (match(type, "^In") || match(type, "^Book")) {
                gsub("^  Chapter", "  01Chapter", a[i])
                gsub("^  Pages", "  02Pages", a[i])
                gsub("^  Title", "  03Title", a[i])
                gsub("^  Publisher", "  04Publisher", a[i])
                gsub("^  Year", "  05Year", a[i])
                gsub("^  Author", "  06Author", a[i])
                gsub("^  Editor", "  07Editor", a[i])
                gsub("^  Volume", "  08Volume", a[i])
                gsub("^  Number", "  09Number", a[i])
                gsub("^  Series", "  10Series", a[i])
                if (!match(type, "^Book"))
                gsub("^  Type", "  11Type", a[i])
                gsub("^  Address", "  12Address", a[i])
                gsub("^  Month", "  13Month", a[i])
                gsub("^  Isbn", "  14Isbn", a[i])
                gsub("^  Note", "  15Note", a[i])
            } else {
                gsub("^  Author", "  01Author", a[i])
                gsub("^  Title", "  02Title", a[i])
                gsub("^  Journal", "  03Journal", a[i])
                gsub("^  Year", "  04Year", a[i])
                gsub("^  Volume", "  05Volume", a[i])
                gsub("^  Number", "  06Number", a[i])
                gsub("^  Pages", "  07Pages", a[i])
                gsub("^  Month", "  08Month", a[i])
                gsub("^  ISSN", "  09ISSN", a[i])
                gsub("^  Note", "  10Note", a[i])
            }
            gsub("^ __", "  20__", a[i])
            # Enforce title case for case insensitive sorting.
            if (match(a[i], "^  [a-z][a-z]*-")) {
                a[i] = toupper(substr(a[i], 1, 3)) substr(a[i], 4)
            }
        }
        # Sort
        asort(a)
        # Restore original field names.
        for(i=1; i<=NF; i++) {
            if (match(type, "^Misc")) {
                gsub("^  01Author", "  Author", a[i])
                gsub("^  02Title", "  Title", a[i])
                gsub("^  03Howpublished", "  Howpublished", a[i])
                gsub("^  04Month", "  Month", a[i])
                gsub("^  05Year", "  Year", a[i])
                gsub("^  06Note", "  Note", a[i])
            } else if (match(type, "^[^}]*thesis")) {
                gsub("^  01Author", "  Author", a[i])
                gsub("^  02Title", "  Title", a[i])
                gsub("^  03School", "  School", a[i])
                gsub("^  04Year", "  Year", a[i])
                gsub("^  05Type", "  Type", a[i])
                gsub("^  06Month", "  Month", a[i])
            } else if (match(type, "^In[cp]")) {
                gsub("^  01Author", "  Author", a[i])
                gsub("^  02Title", "  Title", a[i])
                gsub("^  03Booktitle", "  Booktitle", a[i])
                gsub("^  04Year", "  Year", a[i])
                gsub("^  05Editor", "  Editor", a[i])
                gsub("^  06Volume", "  Volume", a[i])
                gsub("^  07Number", "  Number", a[i])
                gsub("^  08Series", "  Series", a[i])
                gsub("^  09Pages", "  Pages", a[i])
                gsub("^  10Address", "  Address", a[i])
                gsub("^  11Month", "  Month", a[i])
                gsub("^  12Organization", "  Organization", a[i])
                gsub("^  [01]3Publisher", "  Publisher", a[i])
                gsub("^  14Note", "  Note", a[i])
            } else if (match(type, "^In") || match(type, "^Book")) {
                gsub("^  01Chapter", "  Chapter", a[i])
                gsub("^  02Pages", "  Pages", a[i])
                gsub("^  03Title", "  Title", a[i])
                gsub("^  04Publisher", "  Publisher", a[i])
                gsub("^  05Year", "  Year", a[i])
                gsub("^  06Author", "  Author", a[i])
                gsub("^  07Editor", "  Editor", a[i])
                gsub("^  08Volume", "  Volume", a[i])
                gsub("^  09Number", "  Number", a[i])
                gsub("^  10Series", "  Series", a[i])
                gsub("^  11Type", "  Type", a[i])
                gsub("^  12Address", "  Address", a[i])
                gsub("^  13Month", "  Month", a[i])
                gsub("^  14Isbn", "  Isbn", a[i])
                gsub("^  15Note", "  Note", a[i])
            } else {
                gsub("^  01Author", "  Author", a[i])
                gsub("^  02Title", "  Title", a[i])
                gsub("^  03Journal", "  Journal", a[i])
                gsub("^  04Year", "  Year", a[i])
                gsub("^  05Volume", "  Volume", a[i])
                gsub("^  06Number", "  Number", a[i])
                gsub("^  07Pages", "  Pages", a[i])
                gsub("^  08Month", "  Month", a[i])
                gsub("^  09ISSN", "  ISSN", a[i])
                gsub("^  10Note", "  Note", a[i])
            }
            gsub("^  20__", " __", a[i])
            # Restore title case.
            if (match(a[i], "^  [A-Z][a-z]*-")) {
                a[i] = tolower(substr(a[i], 1, 3)) substr(a[i], 4)
            }
        }
    } else {
        loopshift=1 # @ will be on first line.
    }

    # Print record separator.
    if (NR > 1) {
        printf(ORS)
    }

    # Print entry type with citation key.
    if (loopshift) {
        printf("%s", a[1])
    } else {
        printf("%s", a[NF-1])
    }

    # Print fields.
    for (i=1+loopshift; i<NF-1+loopshift; i++) {
        if (a[i] != "") {
            printf("%s%s", OFS, a[i])
        }
    }

    # Print closing bracket.
    printf("%s%s", OFS, a[NF])
}
END {
    if (fieldsort) {
        printf("\n\n")
    } else {
        printf("\n")
    }
}
' \
    | grep -v '^@$'