如何将充满 UNIX 时间字符串的文件转换为人类可读的日期?

发布于 2024-09-02 00:51:18 字数 428 浏览 2 评论 0原文

我正在处理一个充满 unix 时间字符串的文件。我想将它们全部转换为人类可读的。

该文件看起来像这样:

1153335401
1153448586
1153476729
1153494310
1153603662
1153640211

这是脚本:

#! /bin/bash
FILE="test.txt"
cat $FILE | while read line; do
perl -e 'print scalar(gmtime($line)), "\n"'
done

这不起作用。我得到的每行输出都是 Thu Jan 1 00:00:00 1970 。我认为换行符正在被拾取,这就是它不起作用的原因。有什么想法吗?我使用的是 Mac OSX,这有什么不同。

I am processing a file full of unix time strings. I want to convert them all to human readable.

The file looks like so:

1153335401
1153448586
1153476729
1153494310
1153603662
1153640211

Here is the script:

#! /bin/bash
FILE="test.txt"
cat $FILE | while read line; do
perl -e 'print scalar(gmtime($line)), "\n"'
done

This is not working. The output I get is Thu Jan 1 00:00:00 1970 for every line. I think the line breaks are being picked up and that is why it is not working. Any ideas? I'm using Mac OSX is that makes any difference.

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

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

发布评论

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

评论(9

清醇 2024-09-09 00:51:18
$ perl -lne 'print scalar gmtime $_' test.txt
Wed Jul 19 18:56:41 2006
Fri Jul 21 02:23:06 2006
Fri Jul 21 10:12:09 2006
Fri Jul 21 15:05:10 2006
Sat Jul 22 21:27:42 2006
Sun Jul 23 07:36:51 2006
$ perl -lne 'print scalar gmtime $_' test.txt
Wed Jul 19 18:56:41 2006
Fri Jul 21 02:23:06 2006
Fri Jul 21 10:12:09 2006
Fri Jul 21 15:05:10 2006
Sat Jul 22 21:27:42 2006
Sun Jul 23 07:36:51 2006
陈年往事 2024-09-09 00:51:18

由于 $line 使用单引号,因此 bash 不会处理它,因此 $line 被视为(未定义的)Perl 变量而不是 bash 变量。

您不需要 while read bash 循环; Perl 可以使用其 -n 选项自行进行循环。

perl -nE 'say scalar(gmtime($_))' test.txt

(使用 -E 启用 say,它会自动附加换行符)

Because $line is in single quotes, it's not being processed by bash, and so $line is treated as an (undefined) Perl variable rather than a bash variable.

You don't need a while read bash loop; Perl can do the looping itself using its -n option.

perl -nE 'say scalar(gmtime($_))' test.txt

(using -E to enable say, which automatically appends a newline)

樱&纷飞 2024-09-09 00:51:18

不要使用cat

#! /bin/bash
file="test.txt"
while read line
do
    date -d @$line
done < "$file"

Don't use cat.

#! /bin/bash
file="test.txt"
while read line
do
    date -d @$line
done < "$file"
娇纵 2024-09-09 00:51:18

这不是换行符,而是 Perl 脚本中的 $line 与 bash 脚本中的 $line 是不同的变量。您可以尝试:

perl -e "print scalar(gmtime($line)),qq/\\n/"

注意双引号,它允许 bash 进行变量插值。

It's not the line breaks, it's that the $line inside the Perl script is a different variable than the $line in the bash script. You could try:

perl -e "print scalar(gmtime($line)),qq/\\n/"

Note the double-quotes, which allow bash to do variable interpolation.

旧时模样 2024-09-09 00:51:18

不需要 Perl:

awk '{ print strftime("%c", $0) }' somefile.txt

No need for Perl:

awk '{ print strftime("%c", $0) }' somefile.txt
爱的那么颓废 2024-09-09 00:51:18

问题是您没有为 $line 变量分配任何内容,因此它默认为零值,这就是为什么您总是得到 Thu Jan 1 00:00:00 1970 作为输出。

gbacon的答案与 Perl 中的一样流畅。

The issue is that you haven't assigned anything to the $line variable, so it defaults to a zero-value, which is why you always get Thu Jan 1 00:00:00 1970 as an output.

gbacon's answer is about as slick as it gets in Perl.

笑着哭最痛 2024-09-09 00:51:18

GNU 日期/xargs 解决方案:

  xargs -i  date -d "1970-01-01 00:00:00 {} seconds"  </tmp/timestamps 

GNU date/xargs solution:

  xargs -i  date -d "1970-01-01 00:00:00 {} seconds"  </tmp/timestamps 
九歌凝 2024-09-09 00:51:18

这个简单的命令就可以了

 cat time.txt | while read line; do date -ud @$line; done > string.txt

This simple command will do

 cat time.txt | while read line; do date -ud @$line; done > string.txt
萌辣 2024-09-09 00:51:18

不要忘记 Perl 中可以使用 localtime() 代替 gmtime()

因此,如果冬天你不在格林威治,则可以使用 localtime(),例如

Perl 代码:

    my $unixtime = 1417014507;
    my $humantime = localtime($unixtime);
    print "\$humantime = $humantime \n";

输出:

    $humantime = Wed Nov 26 15:08:27 2014 

Not forgetting that localtime() can be used instead of gmtime() in Perl

So, if you're not in Greenwich in Winter, you can use localtime(), e.g.

Perl code:

    my $unixtime = 1417014507;
    my $humantime = localtime($unixtime);
    print "\$humantime = $humantime \n";

output:

    $humantime = Wed Nov 26 15:08:27 2014 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文