2017-08-30 166 views
-2

我有一个for循环递归搜索目录中的文件。从字符串路径检索日期

for FILE in $(find /home/mydir/ -name '*.txt' -or -name '*.zip') 

它们具有以下格式:

/home/mydir/subdir1/subdir2/22280317.txt 

我想提取离开.txt的作为格式2017-03-28之日起6个号码我已经用awk试过不过我有麻烦,因为是我想忽略的两位数字

回答

1

这可能会解决你的问题

find -name "*.txt" -exec sh -c 'f=${0%.txt}; l6=${f: -6}; date -d "${l6: -2}-${l6:2:2}-${l6:0:2}" +"%Y-%m-%d" ' {} \; 

测试结果

# create sample files 
[[email protected] test]$ touch 10280317.txt 
[[email protected] test]$ touch 10170317.txt 
[[email protected] test]$ touch 10170398.txt 

# files created 
[[email protected] test]$ ls *.txt 
10170317.txt 10170398.txt 10280317.txt 

# output using date command which takes care of year 
# remove .txt 
# extract last 6 char 
# input year-mon-date to date command 
[[email protected] test]$ find -name "*.txt" -exec bash -c 'f=${0%.txt}; l6=${f: -6}; date -d "${l6: -2}-${l6:2:2}-${l6:0:2}" +"%Y-%m-%d" ' {} \; 
2017-03-28 
2017-03-17 
1998-03-17 

在情况下,如果你想与日期,那么

[[email protected] tmp]$ pwd 
/tmp 

[[email protected] tmp]$ find -name "*.txt" -exec bash -c 'f=${0%.txt}; l6=${f: -6}; echo $f $(date -d "${l6: -2}-${l6:2:2}-${l6:0:2}" +"%Y-%m-%d") ' {} \; 
./tss/test/10280317 2017-03-28 
./tss/test/10170317 2017-03-17 
./tss/test/10170398 1998-03-17 
一起显示文件名
0

With sed表达式:

find /home/mydir/ -name '*.txt' -or -name '*.zip' | sed -E 's/.*([0-9]{2})([0-9]{2})([0-9]{2})\.txt/20\3-\2-\1/' 
0

假设该文件是相同的格式(22280317.txt)命名为永诺,这是我的解决方案:

awk -v FPAT="([0-9]{2})" 'BEGIN{ FS = "/"; OFS = "-" }{ x = substr($NF, 3, 6); if (patsplit(x, a)) print a[1], a[2], "20"a[3] }' 

打印:

28-03-2017