从存储在bash变量中的文件名中提取日期时间戳的方法是什么?

huangapple go评论60阅读模式
英文:

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=&quot;/opt/project/xxxx/async_transaction__2023-05-28.csv&quot;
date=&quot;${filename##*_}&quot;
date=&quot;${date%.*}&quot;
echo &quot;$date&quot;

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 &quot;${filename}&quot; | grep -o &quot;202[0-9]-[0-1][0-9]-[0-3][0-9]&quot;)
$ 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 &quot;202[0-9]-[0-1][0-9]-[0-3][0-9]&quot; &lt;&lt;&lt; &quot;${filename}&quot;)
$ echo $dateis
2023-05-28

huangapple
  • 本文由 发表于 2023年5月30日 05:26:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定