You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shell脚本需求:提取文件名日期并校验是否未早于前两天

Solution for Date Validation of Files in FFS_FILES.txt

Let's break this down step by step to solve your date validation problem. First, note that your initial awk command only works for the first two filenames—we'll need a more flexible way to extract dates from both formats (MMDDYYYY and MM_DD_YY) in your file list. Then we'll convert those dates to a comparable format, calculate the cutoff date (two days ago), and validate each file.

Step 1: Extract & Standardize Dates from Filenames

We'll use a Bash loop with regex matching to handle both date formats, then convert each date to a Unix timestamp (a numeric representation of time that's easy to compare).

# Initialize an associative array to store filenames and their timestamps
declare -A file_dates

# Process each filename in FFS_FILES.txt
while read -r filename; do
    # Case 1: Match MMDDYYYY format (e.g., 10262017)
    if [[ $filename =~ ([0-9]{8}) ]]; then
        raw_date="${BASH_REMATCH[1]}"
        # Rearrange to MM/DD/YYYY for date command parsing
        formatted_date="${raw_date:0:2}/${raw_date:2:2}/${raw_date:4:4}"
    
    # Case 2: Match MM_DD_YY format (e.g., 10_22_17)
    elif [[ $filename =~ ([0-9]{2})_([0-9]{2})_([0-9]{2}) ]]; then
        month="${BASH_REMATCH[1]}"
        day="${BASH_REMATCH[2]}"
        year="20${BASH_REMATCH[3]}" # Assume 20xx for two-digit years
        formatted_date="${month}/${day}/${year}"
    
    # Handle files where date can't be extracted
    else
        echo "⚠️ Warning: No valid date found in filename: $filename"
        continue
    fi

    # Convert formatted date to Unix timestamp (silence invalid date errors)
    file_timestamp=$(date -d "$formatted_date" +%s 2>/dev/null)
    if [[ -z $file_timestamp ]]; then
        echo "❌ Error: Invalid date '$formatted_date' from filename: $filename"
        continue
    fi

    # Store the timestamp with its filename
    file_dates["$filename"]=$file_timestamp
done < FFS_FILES.txt

Step 2: Calculate the Cutoff Timestamp (Two Days Ago)

Use the date command to get the Unix timestamp for exactly two days before the current date:

# Get timestamp for 2 days ago (adjust timezone if needed with TZ=UTC)
two_days_ago_ts=$(date -d "2 days ago" +%s)

Step 3: Validate Each File's Date

Loop through our associative array and compare each file's timestamp against the cutoff:

echo -e "\n📋 Validation Results:"
for filename in "${!file_dates[@]}"; do
    file_ts=${file_dates[$filename]}
    if [[ $file_ts -lt $two_days_ago_ts ]]; then
        echo "❌ FAIL: $filename (date is earlier than two days ago)"
    else
        echo "✅ PASS: $filename (date is valid)"
    fi
done

Key Notes:

  • Timezone Considerations: If your files use UTC dates, add TZ=UTC before the date commands (e.g., TZ=UTC date -d ...) to avoid timezone mismatches.
  • Year Assumption: For two-digit years (17), we assume 2017—adjust the year="20${BASH_REMATCH[3]}" line if you need to handle 19xx years.
  • Flexibility: This script handles both date formats in your example, and you can extend the regex patterns if you encounter other date formats later.

内容的提问来源于stack exchange,提问作者kunal .Gaurav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:53:17