如何在命令行上获取和格式化昨天的日期?

时间:2021-09-19 01:38:22

I have a Perl script which will run in a cron job on linux suse. It will take as input a log file that was generated yesterday. The filename of the log contains the date (i.e. log.20100209)

我有一个Perl脚本,它将在linux suse上的cron作业中运行。它将输入昨天生成的日志文件作为输入。日志的文件名包含日期(即log.20100209)

Can I send yesterday's date with the format in the prompt? Should I create an additional script to get the date and execute? If so, how can I do that?

我可以使用提示中的格式发送昨天的日期吗?我应该创建一个额外的脚本来获取日期并执行吗?如果是这样,我该怎么办?

Thanks

谢谢

perl myscript.pl -f log.20100209

Edit

编辑

Thanks for your help

谢谢你的帮助

It worked with:

它适用于:

perl myscript.pl -f log.`date --date='yesterday' '+%Y%m%d'`

3 个解决方案

#1


28  

GNU date:

GNU日期:

date --date='yesterday' '+%Y%m%d'

#2


1  

You can get yesterday's date like this:

你可以像这样得到昨天的日期:

perl -we'@a=localtime(time-24*3600);printf "%04d%02d%02d", $a[5]+1900, $a[4]+1, $a[3]'

You can use this when calling your script at the prompt:

您可以在提示符处调用脚本时使用此选项:

perl myscript.pl -f log.`perl -we'@a=localtime(time-24*3600);printf "%04d%02d%02d", $a[5]+1900, $a[4]+1, $a[3]'`

But this is unreadable, and I suggest you write a proper script that calculates yesterday's date, and then calls myscript.pl.

但这是不可读的,我建议你写一个正确的脚本来计算昨天的日期,然后调用myscript.pl。

#3


0  

You shouldn't need to send yesterday's date as an additional parameter. You can already get it using two other methods:

您不应该将昨天的日期作为附加参数发送。您可以使用其他两种方法获取它:

  1. Perl's built-in time(), localtime(), or gmtime() functions will give you the current date and time, and you can work with that to determine yesterday's date.
  2. Perl的内置time(),localtime()或gmtime()函数将为您提供当前日期和时间,您可以使用它来确定昨天的日期。
  3. It is already included in the name of your log file, so you can parse the file name to get the date into the format you need.
  4. 它已包含在日志文件的名称中,因此您可以解析文件名以获取所需格式的日期。

Perl has a lot of modules to work with dates and times, depending exactly what you need to do.

Perl有很多模块可以处理日期和时间,具体取决于您需要做什么。

#1


28  

GNU date:

GNU日期:

date --date='yesterday' '+%Y%m%d'

#2


1  

You can get yesterday's date like this:

你可以像这样得到昨天的日期:

perl -we'@a=localtime(time-24*3600);printf "%04d%02d%02d", $a[5]+1900, $a[4]+1, $a[3]'

You can use this when calling your script at the prompt:

您可以在提示符处调用脚本时使用此选项:

perl myscript.pl -f log.`perl -we'@a=localtime(time-24*3600);printf "%04d%02d%02d", $a[5]+1900, $a[4]+1, $a[3]'`

But this is unreadable, and I suggest you write a proper script that calculates yesterday's date, and then calls myscript.pl.

但这是不可读的,我建议你写一个正确的脚本来计算昨天的日期,然后调用myscript.pl。

#3


0  

You shouldn't need to send yesterday's date as an additional parameter. You can already get it using two other methods:

您不应该将昨天的日期作为附加参数发送。您可以使用其他两种方法获取它:

  1. Perl's built-in time(), localtime(), or gmtime() functions will give you the current date and time, and you can work with that to determine yesterday's date.
  2. Perl的内置time(),localtime()或gmtime()函数将为您提供当前日期和时间,您可以使用它来确定昨天的日期。
  3. It is already included in the name of your log file, so you can parse the file name to get the date into the format you need.
  4. 它已包含在日志文件的名称中,因此您可以解析文件名以获取所需格式的日期。

Perl has a lot of modules to work with dates and times, depending exactly what you need to do.

Perl有很多模块可以处理日期和时间,具体取决于您需要做什么。