从linux中的文件名末尾提取日期

时间:2017-10-20 09:25:01

标签: linux shell

我正在使用shell程序,并且有一些带有这些模式的文件:

abc_def_ghjuy_2017-09-12_15-30-40.txt
abc_def_khujdr_bv_bmg_2017-09-12_15-31-40.txt
abc_def_pkiljoy_vcdfr_2017-09-12_15-32-40.txt
abc_def_charidp_bf_mj_fr_2017-09-12_15-32-40.txt

如何在句点之前提取文件名位置2的日期并将其保存在变量中?

1 个答案:

答案 0 :(得分:-1)

您可以使用这个简单的脚本。在find.sh

#!/bin/bash
ls abc_def_*-* | sed 's/abc_def\(.*\)\.txt/\1/' >> file
sed -r 's/.*(.{8})/\1/' file >> file5
mapfile -t array < file5
sudo rm -r file

运行此脚本bash.sh。现在,您想要的字段被放置为变量。

$ echo "${array[1]}"
$ echo "${array[2]}"
$ echo "${array[3]}"
....

see the outputs 如果您有更好的解决方案,请告诉我......