从字符串路径

时间:2017-08-30 10:10:42

标签: string awk sed grep

我有一个for循环,以递归方式在目录中搜索文件。

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

它们具有以下格式:

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

我想将.txt左侧的6个数字作为2017-03-28格式的日期提取我尝试使用awk但是因为有两位数而遇到麻烦在一开始我想忽略

3 个答案:

答案 0 :(得分: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
[akshay@localhost test]$ touch 10280317.txt 
[akshay@localhost test]$ touch 10170317.txt 
[akshay@localhost test]$ touch 10170398.txt 

# files created
[akshay@localhost 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
[akshay@localhost 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

如果您想显示文件名和日期,那么

[akshay@localhost tmp]$ pwd
/tmp

[akshay@localhost 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

答案 1 :(得分:0)

使用 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/' 

答案 2 :(得分: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