-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.sh
executable file
·47 lines (41 loc) · 1.85 KB
/
sort.sh
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
#!/bin/bash
# Set the input and output directories
input_dir="/lnet/work/projects/atrium/ARUP_HDD_PDF"
output_dir="/lnet/work/people/lutsai/pythonProject/pages/train_final"
input_csv="/lnet/work/people/lutsai/pythonProject/pages/input.csv"
# Ensure the output directory exists
mkdir -p "$output_dir"
# Read the CSV file line by line (skip the header row if necessary)
# CSV file should have exactly 3 columns for file, page, and category
tail -n +2 "$input_csv" | while IFS=',' read -r filename page_number category; do
# Create category subdirectory inside output directory if it doesn't exist
category_dir="$output_dir/$category"
mkdir -p "$category_dir"
# Check if subdirectory exists in input directory
input_subdir="$input_dir/$filename"
if [ -d "$input_subdir" ]; then
# Get the number of files in the input subdirectory
file_count=$(ls -1q "$input_subdir"/*.png 2>/dev/null | wc -l)
# Pad the page number with zeros to match the file name format
pn=$(printf "%0${#file_count}d" "$page_number")
# Find the file ending with the padded page number
file_to_copy=$(find "$input_subdir" -type f -name "*-$pn.png" -print -quit)
# Copy the file to the category subdirectory
if [ -n "$file_to_copy" ]; then
cp "$file_to_copy" "$category_dir/"
else
echo "File ending with $page_number not found in $input_subdir."
fi
else
# Use 'onepagers' as the default subdirectory
default_subdir="$input_dir/onepagers"
# Find the file starting with the filename and ending with the page number
file_to_copy=$(find "$default_subdir" -type f -name "$filename-$page_number.png" -print -quit)
if [ -n "$file_to_copy" ]; then
cp "$file_to_copy" "$category_dir/"
else
echo "File starting with $filename and ending with $page_number not found in $default_subdir."
fi
fi
done
echo "Processing completed."