如何在 bash 中将 add x days 转换为 var

如何在 bash 中将 add x days 转换为 var

我有一个包含许多行的文件,如下所示:

0 D FakeSip          192.169.192.192                      jan/26/2022 17:10:31

我想导出 IP 地址、日期,然后在日期上添加 10 天,这将给出到期日期。我已经获得了 IP,并且插入的日期没有问题,但在日期上添加 10 天并导出却很痛苦。如果您能提供一些帮助,我将不胜感激?

cat FakeSip.txt|awk --posix '$4 ~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/ { print " IP Address "$4 " Date Identified "$5 " Expiration " (date -d  $5+10 days);}' 

这是上面给出的输出

IP Address 192.241.212.118 Date Identified jan/25/2022 Expiration 010

所需的输出如下所示:

IP Address 192.169.192.192 Date Identified jan/26/2022 Expiration Feb/05/2022

答案1

  LANG=C LC_ALL=C awk '
    $4 ~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/
      {
        dvar = $5;
        gsub("[^[:digit:][:alpha:]]+"," ",dvar); # turn any special character into space to make date parseable and protect against command injection
        cmd = "date -d \"" d"+10 days\" +%b/%d/%Y";
        cmd | getline expire; close(cmd);
        print " IP Address "$4 " Date Identified "$5 " Expiration " expire
      }
    ' FakeSip.txt

由于信用这个答案,我从中抄写了将命令输出分配给变量的代码:

答案2

zsh

#! /bin/zsh -
zmodload zsh/datetime
read -r x y z ip date time < FakeSip.txt &&
  LC_ALL=C strftime -rs t     '%b/%d/%Y %H:%M:%S' "$date $time" &&
  LC_ALL=C strftime  -s expire %b/%d/%Y           $((t+10*24*60*60)) &&
  print -r IP Address $ip Date Identified $date Expiration $expire

LC_ALL=C是为了强制这些月份缩写以英语解释/输出。删除它,以便以用户的语言解释/输出它们。

日期按当地时间解释,我们添加 864000 秒。这并不总是与 10 天相同,具体取决于您如何定义以及涉及夏令时的地方。

将要转换$expire为小写的月份名称替换为${(L)expire}(或$expire:l类似)(而不是匹配该样式)。tcshLfebFebjan

答案3

聚会迟到了,但我喜欢约会问题。

用perl

use strict;
use warnings;
use Time::Piece;

# regex stolen from Regexp::Common::net
# https://metacpan.org/pod/Regexp::Common::net

my $re_ipv4 = qr/(?:(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))/o;
my $fmt = '%b/%d/%Y';

while (<>) {
    my @F = split ' ';
    if ($F[3] =~ /^$re_ipv4$/) {
        my $dt = Time::Piece->strptime($F[4], $fmt);
        my $exp = ($dt + 86400 * 10)->strftime($fmt);
        print "IP Address $F[3] Date Identified $F[4] Expiration " . lc($exp) . "\n";
    }
}

我不太喜欢添加几秒来表示天,但此代码不支持时区,因此夏令时转换不起作用。为了“正确”地做到这一点,我们需要DateTimeDateTime::Format::Strptime来自 CPAN 的模块。

这导致

$ perl add10.pl file
IP Address 192.169.192.192 Date Identified jan/26/2022 Expiration feb/05/2022

或者红宝石,

require 'date'

re_ipv4 = Regexp.new("^(?:(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))$")
fmt = '%b/%d/%Y'

File.new(ARGV.shift).each do |line|
  fields = line.split
  if fields[3].match?(re_ipv4)
    dt = Date.strptime(fields[4], fmt)
    exp = (dt + 10).strftime(fmt).downcase
    puts "IP Address #{fields[3]} Date Identified #{fields[4]} Expiration #{exp}"
  end
end

$ ruby add10.rb file
IP Address 192.169.192.192 Date Identified jan/26/2022 Expiration feb/05/2022

相关内容