从7天列表开始每天最后生成的文件列表

时间:2012-01-07 06:30:57

标签: shell unix

我有以下格式的文件列表:

Group_2012_01_06_041505.csv
Region_2012_01_06_041508.csv
Region_2012_01_06_070007.csv
XXXX_YYYY_MM_DD_HHMMSS.csv

从过去7天列表中为每组每天编制上次生成的文件列表的最佳方法是什么?

适用于HP-UX的版本

for d in 6 5 4 3 2 1 0
do
    DATES[d]=$(perl -e "use POSIX;print strftime '%Y_%m_%d%',localtime time-86400*$d;")
done

for group in `ls *.csv | cut -d_ -f1 | sort -u`
do
    CSV_FILES=$working_dir/*.csv
    if [ ! -f $CSV_FILES ]; then
        break # if no file exists do not attempt processing
    fi
    for d in "${DATES[@]}"
    do
        file_nm=$(ls ${group}_$d* 2>>/dev/null | sort -r | head -1)
        if [ "$file_nm" != "" ]
        then
            # Process file
        fi
    done
done

1 个答案:

答案 0 :(得分:0)

您可以明确地迭代组/时间组合:

for d in {1..6}
do
    DATES[d]=`gdate +"%Y_%m_%d" -d "$d day ago"`
done

for group in `ls *csv | cut -d_ -f1 | sort -u`
do
    for d in "${DATES[@]}"
    do
        echo "$group $d: " `ls ${group}_$d* 2>>/dev/null | sort -r | head -1`
    done
done

为您的示例数据集输出以下内容:

Group 2012_01_06:  Group_2012_01_06_041505.csv
Group 2012_01_05: 
Group 2012_01_04: 
Group 2012_01_03: 
Group 2012_01_02: 
Group 2012_01_01: 
Region 2012_01_06:  Region_2012_01_06_070007.csv
Region 2012_01_05: 
Region 2012_01_04: 
Region 2012_01_03: 
Region 2012_01_02: 
Region 2012_01_01: 
XXXX 2012_01_06: 
XXXX 2012_01_05: 
XXXX 2012_01_04: 
XXXX 2012_01_03: 
XXXX 2012_01_02: 
XXXX 2012_01_01: 

区域2012_01_06未显示Region_2012_01_06_041508.csv,因为它早于Region_2012_01_06_070007.csv