Shell 脚本字符串操作

发布于 2024-10-15 15:00:14 字数 334 浏览 6 评论 0原文

我正在尝试用人类可读的时间戳替换字符串中的纪元时间戳。我知道如何将纪元转换为我需要的时间格式(并且一直在手动执行此操作),尽管我无法弄清楚如何在字符串中替换它(通过脚本)。

该字符串是文件名,例如 XXXX-XXX-2011-01-25-3.6.2-record.pb.1296066338.gz(纪元以粗体显示)。

我一直在使用以下 gawk 代码转换时间戳:

gawk 'BEGIN{print strftime("%Y%m%d.%k%M",1296243507)}'

我通常不熟悉 bash 脚本。谁能帮我朝正确的方向推动?

谢谢。

I'm trying to replace an epoch timestamp within a string, with a human readable timestamp. I know how to convert the epoch to the time format I need (and have been doing so manually), though I'm having trouble figuring out how to replace it within the string (via script).

The string is a file name, such as XXXX-XXX-2011-01-25-3.6.2-record.pb.1296066338.gz (epoch is bolded).

I've been converting the timestamp with the following gawk code:

gawk 'BEGIN{print strftime("%Y%m%d.%k%M",1296243507)}'

I'm generally unfamiliar with bash scripting. Can anyone give me a nudge in the right direction?

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一城柳絮吹成雪 2024-10-22 15:00:15

可以使用它。

date -d '@1296066338' +'%Y%m%d.%k%M'

如果您不想调用 awk,

You can use this

date -d '@1296066338' +'%Y%m%d.%k%M'

in case you don't want to invoke awk.

ヅ她的身影、若隐若现 2024-10-22 15:00:15

所有文件名的格式都相同吗?具体来说, ”。” +纪元+“.gz”?

如果是这样,您可以使用许多不同的路线。这是 sed 的一个:

$ echo "XXXX-XXX-2011-01-25-3.6.2-record.pb.1296066338.gz" | sed 's/.*\.\([0-9]\+\)\.gz/\1/'
1296066338

这样就可以提取纪元,然后将其发送到您的 gawk 命令。类似于:

#!/bin/bash

...
epoch=$( echo "XXXX-XXX-2011-01-25-3.6.2-record.pb.1296066338.gz" | sed 's/.*\.\([0-9]\+\)\.gz/\1/' )
readable_timestamp=$( gawk "BEGIN{print strftime(\"%Y%m%d.%k%M\",${epoch})}" )

然后使用您想要替换文件名中的数字的任何方法。您可以再次通过 sed 发送它,但您不想保存纪元,而是希望保存文件名的其他部分。

编辑:
为了更好地衡量,我的机器上有一个工作示例:

#!/bin/bash

filename="XXXX-XXX-2011-01-25-3.6.2-record.pb.1296066338.gz"

epoch=$( echo ${filename} | sed 's/.*\.\([0-9]\+\)\.gz/\1/' )
readable_timestamp=$( gawk "BEGIN{print strftime(\"%Y%m%d.%k%M\",${epoch})}" )

new_filename=$( echo ${filename} | sed "s/\(.*\.\)[0-9]\+\(\.gz\)/\1${readable_timestamp}\2/" )

echo ${new_filename}

Are all filenames the same format? Specifically, "." + epoch + ".gz"?

If so, you can use a number of different routes. Here's one with sed:

$ echo "XXXX-XXX-2011-01-25-3.6.2-record.pb.1296066338.gz" | sed 's/.*\.\([0-9]\+\)\.gz/\1/'
1296066338

So that extracts the epoch, then send it to your gawk command. Something like:

#!/bin/bash

...
epoch=$( echo "XXXX-XXX-2011-01-25-3.6.2-record.pb.1296066338.gz" | sed 's/.*\.\([0-9]\+\)\.gz/\1/' )
readable_timestamp=$( gawk "BEGIN{print strftime(\"%Y%m%d.%k%M\",${epoch})}" )

Then use whatever method you want to replace the number in the filename. You can send it through sed again, but instead of saving the epoch, you would want to save the other parts of the filename.

EDIT:
For good measure, a working sample on my machine:

#!/bin/bash

filename="XXXX-XXX-2011-01-25-3.6.2-record.pb.1296066338.gz"

epoch=$( echo ${filename} | sed 's/.*\.\([0-9]\+\)\.gz/\1/' )
readable_timestamp=$( gawk "BEGIN{print strftime(\"%Y%m%d.%k%M\",${epoch})}" )

new_filename=$( echo ${filename} | sed "s/\(.*\.\)[0-9]\+\(\.gz\)/\1${readable_timestamp}\2/" )

echo ${new_filename}
傲世九天 2024-10-22 15:00:15

您可以使用 Bash 的字符串操作和 AWK 的变量传递来避免对 sed 进行任何调用或进行任何引号转义。

#!/bin/bash
file='XXXX-XXX-2011-01-25-3.6.2-record.pb.1296066338.gz'
base=${file%.*.*}     #    XXXX-XXX-2011-01-25-3.6.2-record.pb
epoch=${file#$base}   #    1296066338.gz
epoch=${epoch%.*}     #    1296066338
# we can extract the extension similarly, unless it's safe to assume it's ".gz"
humantime=$(gawk -v "epoch=$epoch" 'BEGIN{print strftime("%Y%m%d.%k%M",epoch)}')
newname=$base.$humantime.gz
echo "$newname"

结果:

XXXX-XXX-2011-01-25-3.6.2-record.pb.20110126.1225.gz

You can use Bash's string manipulation and AWK's variable passing to avoid having to make any calls to sed or do any quote escaping.

#!/bin/bash
file='XXXX-XXX-2011-01-25-3.6.2-record.pb.1296066338.gz'
base=${file%.*.*}     #    XXXX-XXX-2011-01-25-3.6.2-record.pb
epoch=${file#$base}   #    1296066338.gz
epoch=${epoch%.*}     #    1296066338
# we can extract the extension similarly, unless it's safe to assume it's ".gz"
humantime=$(gawk -v "epoch=$epoch" 'BEGIN{print strftime("%Y%m%d.%k%M",epoch)}')
newname=$base.$humantime.gz
echo "$newname"

Result:

XXXX-XXX-2011-01-25-3.6.2-record.pb.20110126.1225.gz
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文