试图编写时区差异的脚本

时间:2017-09-14 04:45:23

标签: bash shell

我是linux脚本的新手,只是想写一个脚本来自动化时区不同。

我正在解决的问题:

  • 搜索名为" a"的两个时间字段和" b"来自给定的文件
  • 找到它们之间的区别" c = b-a"
  • 并相应地指定时区:
    • if" c"在04:00:00到05:00:00之间,然后回复" Newyork"
    • elseif" c"在05:00:00到06:00:00之间然后回复" Chicago"
    • elseif" c"在06:00:00到07:00:00之间,然后回复" Denver"

真实场景:

这是我的档案:

DEBUG   2017-09csssss-03 01:03:47,956 logging_user=LOCAL SERVICE,
windows_event_id=2105, windows_user_name=LOCAL SERVICE, [**create_dt=
07:09:33**][1], syslog_parser_LogHeaderType=SYSLOG_3164_0,
destination_host_name=WREMOTE029QPG.xxxxx.COM,
log_acceptor_protocol=UDP,
windows_event_source=Microsoft-Windows-DriverFrameworks-UserMode,
option56=Well Known Group, log_acceptor_port=514,
syslog_severityText=INFO, vendor_severity=1,
windows_computer_name=WREMOTE029QPG.xxxxx.COM, event_ct=1,
machine_ip=192.168.0.200, target_resource=LOCAL SERVICE,
windows_event_record_number=3542, proxy_machine_ip=192.168.157.1,
proxy_machine_port=49333, LoggingProtocol=SYSLOG_SERVER,
reporting_sensor=Sensor_onbox_UDP_514,
windows_source_eventlog=Microsoft-Windows-DriverFrameworks-UserMode/Operational,
[**event_dt=02:09:30**][2], user_name=LOCAL SERVICE,
log_acceptor_type=SYSLOG_SERVER,
logging_device_name=WREMOTE029QPG.xxxxx.COM, category_id=30007601,
source_host_name=WREMOTE029QPG.xxxxx.COM,
EventClassName=symc_windows_eventlog,parse severity=1, 


  [1]: http://value%201   [2]: http://value%202

从上面的文件中,我试图找到时区。

搜索"event_dt"="create_dt="的值,找到差异如下

timezone = 07:09:33 - 02:09:30 = 05:00:03就像那样

如果" timezone"在04:00:00到05:00:00之间,然后回声"纽约"

elseif" if" c"在05:00:00到06:00:00之间,然后回复" Chicago"

最后输出应该是:芝加哥

我过去一周都在尝试这个,但没有运气。

请帮助我:)

1 个答案:

答案 0 :(得分:0)

对于像这样的事情,Bash不是理想的解决方案。 Bash的字符串解析功能充其量只是基本的。它可以完成,但会非常复杂,特别是如果您要解析的文本没有标准化。像这样的问题正是Larry Wall发明Perl的原因。所以,在Perl中你可以尝试这样的东西(没有测试稳健性):

#!/usr/bin/perl

open( my $fh, "<", "f.txt") or die "cannot open file: $!\n";
my $line = <$fh>;
close($fh);

my @fields = split(',', $line);

foreach my $elm (@fields) {
    if ($elm =~ /create_dt=(.*)/) {
        $created = $1;
        $created =~ s/ *//;
    }
    if ($elm =~ /event_dt=(.*)/) {
        $event = $1;
        $event =~ s/ *//;
    }
}

my @ctimes = split(':', $created);
my @etimes = split(':', $event);
my $diff = $ctimes[0] - $etimes[0];

if($diff == 4) {
    print "New York\n";
} elsif($diff == 5) {
    print "Chicago\n";
} elsif($diff == 6) {
    print "Denver\n";
} else {
    print "could not determine timezone";
}

在Python中它可能是这样的:

#!/usr/bin/python

from datetime import timedelta

with open('f.txt') as f:
    l = f.readline()
f.close()

times = [s for s in l.split(',') if '_dt' in s]
ctimes = ((times[0]).split('=')[1]).strip().split(':')
etimes = ((times[1]).split('=')[1]).strip().split(':')

t1 = timedelta(hours=int(ctimes[0]), minutes=int(ctimes[1]), seconds=int(ctimes[2]))
t2 = timedelta(hours=int(etimes[0]), minutes=int(etimes[1]), seconds=int(etimes[2]))

diff = ((t1 - t2).seconds//3600)

if diff == 4:
    print("New York")
elif diff == 5:
    print("Chicago")
elif diff == 6:
    print("Denver")