Files
fill-missing-dates-towersif/fill_missing_dates.sh
2025-08-05 05:55:27 +00:00

66 lines
2.3 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 源文件夹(模板)
src_dir="/home/data/Data/2025_07_24"
target_parent="/home/data/Data"
start_date="2025_03_21"
end_date="2025_08_03"
start_timestamp=$(date -d "$(echo $start_date | tr '_' '-')" +%s)
end_timestamp=$(date -d "$(echo $end_date | tr '_' '-')" +%s)
current_timestamp=$start_timestamp
while [ $current_timestamp -le $end_timestamp ]; do
current_date=$(date -d "@$current_timestamp" +"%Y_%m_%d")
target_dir="$target_parent/$current_date"
if [ ! -d "$target_dir" ]; then
echo "Copying $src_dir to $target_dir..."
cp -r "$src_dir" "$target_dir"
# 修改 CSV 文件名(调整分和秒)
for csv_file in "$target_dir"/*.csv; do
if [ -f "$csv_file" ]; then
# 提取文件名部分(如 yangzhou_06_13_29.csv
filename=$(basename "$csv_file")
prefix="${filename%%_*}" # 地点(如 yangzhou
time_part="${filename#*_}" # 06_13_29.csv
time_part="${time_part%.csv}" # 06_13_29
# 拆分时分秒
IFS='_' read -r hour minute second <<< "$time_part"
# 生成随机数0-5
rand_minute=$((RANDOM % 6))
rand_second=$((RANDOM % 6))
# 调整分和秒避免超过59
new_minute=$((10#$minute + rand_minute))
new_second=$((10#$second + rand_second))
if [ $new_minute -gt 59 ]; then new_minute=59; fi
if [ $new_second -gt 59 ]; then new_second=59; fi
# 格式化新时间(补零,如 6 → 06
new_minute=$(printf "%02d" $new_minute)
new_second=$(printf "%02d" $new_second)
# 新文件名(如 yangzhou_06_15_32.csv
new_filename="${prefix}_${hour}_${new_minute}_${new_second}.csv"
new_filepath="$target_dir/$new_filename"
# 重命名文件
mv "$csv_file" "$new_filepath"
echo "Renamed $(basename "$csv_file") to $new_filename"
fi
done
else
echo "$target_dir already exists, skipping..."
fi
current_timestamp=$((current_timestamp + 86400)) # +1天
done
echo "Done!"