-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarcsort
More file actions
executable file
·68 lines (51 loc) · 1.61 KB
/
marcsort
File metadata and controls
executable file
·68 lines (51 loc) · 1.61 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
filelist=("$@")
infile="${filelist[0]}"
if [[ -z ${infile} ]];then echo "You must supply a file name, wildcards ok";fi
fileroot=$(echo "${infile}" | sed 's/\.....\?$//')
outfile="${fileroot}_sorted.mrc"
read -r -d '' awkscript << "ENDOFAWK"
#!/usr/bin/awk -f
BEGIN {
if(length("а") != 2) {
badawk = 1
printf("Your version of awk does not support marcadd -- you need a version that supports the -b switch\\n")
exit
}
}
{
### no error correction or accommodation for bad directories -- use marcfix first
leader=substr($0,1,24)
base_address=substr(leader,13, 5) + 0
record_content=substr($0, base_address + 1)
directory=substr($0,25, base_address - 25)
directory_length=length(directory)
dir_pos = 0
new_directory = ""
for (i=1; i<=directory_length; i=i+12) {
dir_pos++
entry = substr(directory, i, 12)
directory_arr[dir_pos] = entry
}
num_entries = asort(directory_arr)
for (i=1; i<=num_entries; i++) {
new_directory = new_directory""directory_arr[i]
}
new_directory = new_directory""OFS
delete directory_arr
print leader""new_directory""record_content > OUTFILE
if (NR % 10000 == 0){ printf "Records processed: %d \\r", NR}
}
END {
ORS="\\n"
print "";print NR " records were output to "OUTFILE
}
ENDOFAWK
echo -e "${awkscript}" > tmp_marcsort
chmod 700 tmp_marcsort
for file in "${filelist[@]}";do
fileroot=$(echo "${file}" | sed 's/\.....\?$//')
outfile="${fileroot}_sorted.mrc"
awk -v RS=$'\x1d' -v ORS=$'\x1d' -v FS=$'\x1e' -v OFS=$'\x1e' -v OUTFILE="${outfile}" -b -f tmp_marcsort "${file}"
done
sleep 1
rm -f tmp_marcsort