beyarkay commited on
Commit ·
1c339c2
1
Parent(s): 1462867
Add chronicling america flattening script
Browse files
src/flatten_chronicling_america.sh
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env bash
|
| 2 |
+
set -Eeuo pipefail
|
| 3 |
+
|
| 4 |
+
# Usage: ./flatten_chronam.sh az_bentonite_ver02
|
| 5 |
+
BATCH_DIR="${1:?need batch dir}"
|
| 6 |
+
|
| 7 |
+
# 1) Collapse .../YYYY/MM/DD → .../YYYY-MM-DD
|
| 8 |
+
if find -E "$BATCH_DIR" -type d -depth -regex '.*/[0-9]{4}/[0-9]{2}/[0-9]{2}$' -print0 >/dev/null 2>&1; then
|
| 9 |
+
FIND_DATE=(find -E "$BATCH_DIR" -type d -depth -regex '.*/[0-9]{4}/[0-9]{2}/[0-9]{2}$' -print0)
|
| 10 |
+
else
|
| 11 |
+
FIND_DATE=(find "$BATCH_DIR" -regextype posix-extended -type d -depth -regex '.*/[0-9]{4}/[0-9]{2}/[0-9]{2}$' -print0)
|
| 12 |
+
fi
|
| 13 |
+
|
| 14 |
+
"${FIND_DATE[@]}" | while IFS= read -r -d '' d; do
|
| 15 |
+
y=$(basename "$(dirname "$(dirname "$d")")")
|
| 16 |
+
m=$(basename "$(dirname "$d")")
|
| 17 |
+
dd=$(basename "$d")
|
| 18 |
+
root=$(dirname "$(dirname "$(dirname "$d")")")
|
| 19 |
+
tgt="$root/$y-$m-$dd"
|
| 20 |
+
[[ -e "$tgt" ]] || mv "$d" "$tgt"
|
| 21 |
+
rmdir -p "$(dirname "$d")" 2>/dev/null || true
|
| 22 |
+
done
|
| 23 |
+
|
| 24 |
+
# 2) Flatten .../YYYY-MM-DD/ed-*/seq-*/ocr.txt → .../YYYY-MM-DD/ed-<n>-seq-<m>-ocr.txt
|
| 25 |
+
find "$BATCH_DIR" -type f -name 'ocr.txt' -path '*/sn*/????-??-??/ed-*/seq-*/ocr.txt' -print0 \
|
| 26 |
+
| while IFS= read -r -d '' f; do
|
| 27 |
+
seq_dir="$(dirname "$f")"
|
| 28 |
+
ed_dir="$(dirname "$seq_dir")"
|
| 29 |
+
date_dir="$(dirname "$ed_dir")"
|
| 30 |
+
ed="$(basename "$ed_dir")" # ed-1
|
| 31 |
+
seq="$(basename "$seq_dir")" # seq-1
|
| 32 |
+
tgt="$date_dir/$ed-$seq-ocr.txt"
|
| 33 |
+
[[ -e "$tgt" ]] || mv "$f" "$tgt"
|
| 34 |
+
rmdir "$seq_dir" 2>/dev/null || true
|
| 35 |
+
rmdir "$ed_dir" 2>/dev/null || true
|
| 36 |
+
done
|
| 37 |
+
|