[英]Extract date from the end of filename in linux
我正在使用 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 中的日期並將其保存在變量中?
您可以使用這個簡單的腳本。 在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]}"
....
查看輸出如果您有更好的解決方案,請告訴我...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.