英文:
how do I extract a date stamp from a file name stored in a bash variable
问题
Sure, here's the translated code part:
我有一个名为 $filename 的 bash 变量,内容如下:
[rmerritt@server]$ echo $filename
/opt/project/xxxx/async_transaction__2023-05-28.csv
我希望能够提取这个日期时间戳 2023-05-28 并将它存储在另一个 bash 变量 $dateis 中,以便我可以执行以下操作:
cp /opt/project/xxxx/*`echo $dateis`*.csv|/opt/project/jdev/data/landing/landing_xxxx/
即将具有该日期时间戳的所有 csv 文件复制到另一个目录。您有没有办法在一行中完成这个操作?我尝试使用 awk,但未成功:
cp `ls -Art /opt/project/xxxx/*__*.csv` /opt/project/jdev/data/landing/landing_xxxx/
Please note that this code assumes you've already extracted the date stamp and stored it in the $dateis
variable correctly. If you need further assistance with the code logic, please let me know.
英文:
I have a bash variable $filename that looks like this
[rmerritt@server]$ echo $filename
/opt/project/xxxx/async_transaction__2023-05-28.csv
what I am hoping to do is extract that date stamp 2023-05-28 and store it in another bash variable $dateis that allows me to do this
cp /opt/project/xxxx/*`echo $dateis`*.csv|/opt/project/jdev/data/landing/landing_xxxx/
I.E. copy all csv files with that datestamp to another directory any idea how I could do this in a oneliner ? I have tried using awk
cp `ls -Art /opt/project/xxxx/*__*.csv` /opt/project/jdev/data/landing/landing_xxxx/
答案1
得分: 2
Sure, here is the translation:
如果你确定这始终是确切的格式,你可以执行以下操作:
$ dateis=${filename: -14:-4}
$ echo $dateis
2023-05-28
Please note that the code part remains in English.
英文:
If you are certain this is always the exact format, you can do:
$ dateis=${filename: -14:-4}
$ echo $dateis
2023-05-28
答案2
得分: 1
使用GNU bash及其内置的参数扩展:
文件名="/opt/project/xxxx/async_transaction__2023-05-28.csv"
日期="${filename##*_}"
日期="${date%.*}"
输出:
<pre>
2023-05-28
</pre>
英文:
With GNU bash and its builtin parameter expansion:
filename="/opt/project/xxxx/async_transaction__2023-05-28.csv"
date="${filename##*_}"
date="${date%.*}"
echo "$date"
Output:
<pre>
2023-05-28
</pre>
答案3
得分: 0
以下是您要翻译的内容:
您可以使用 grep -o
轻松提取日期:
$ filename=/opt/project/xxxx/async_transaction__2023-05-28.csv
$ dateis=$(echo "${filename}" | grep -o "202[0-9]-[0-1][0-9]-[0-3][0-9]")
$ echo $dateis
2023-05-28
根据需要更改正则表达式以保持简单并适用于所有情况。
编辑: 如果您有大量文件,遵循 @user1934428 的建议并避免调用额外的程序/管道是明智的。要进行最小更改以修复这个问题,可以使用 "Here String":
$ filename=/opt/project/xxxx/async_transaction__2023-05-28.csv
$ dateis=$(grep -o "202[0-9]-[0-1][0-9]-[0-3][0-9]" <<< "${filename}")
$ echo $dateis
2023-05-28
英文:
You may use grep -o
to extract the date easily:
$ filename=/opt/project/xxxx/async_transaction__2023-05-28.csv
$ dateis=$(echo "${filename}" | grep -o "202[0-9]-[0-1][0-9]-[0-3][0-9]")
$ echo $dateis
2023-05-28
Change the regular expression as you wish to keep it simple and work for all your cases.
EDIT: If you have a lot of files, it's wise to follow @user1934428 suggestion and avoid calling additional programs/piping. One minimal change to fix that would be to use "Here String" instead:
$ filename=/opt/project/xxxx/async_transaction__2023-05-28.csv
$ dateis=$(grep -o "202[0-9]-[0-1][0-9]-[0-3][0-9]" <<< "${filename}")
$ echo $dateis
2023-05-28
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论