输入:
1,aaaa,2018-11-03
2,bbbb,2018-02-03
预期输出
脚本应将日期视为MST7MDT格式并将其转换为UTC:
1,aaaa,2018-11-03 06:00:00
2,bbbb,2018-02-03 07:00:00
答案 0 :(得分:0)
您没有指定您的操作系统。假设你有GNU日期:
(
IFS=,
while read -ra fields; do
t=$(TZ=America/Denver date -d "${fields[-1]}" "+%s")
fields[-1]=$(TZ=UTC date -d "@$t" "+%F %T")
echo "${fields[*]}"
done
) <<END
1,aaaa,2018-11-03
2,bbbb,2018-02-03
END
1,aaaa,2018-11-03 06:00:00
2,bbbb,2018-02-03 07:00:00
我喜欢Perl的DateTime
图书馆生态系统:
perl -E '
use DateTime::Format::Strptime;
my $dateparser = DateTime::Format::Strptime->new(
pattern => "%Y-%m-%d",
time_zone => "America/Denver"
);
while (<>) {
chomp;
my @fields = split /,/;
my $datetime = $dateparser->parse_datetime( $fields[-1] );
$fields[-1] = $datetime->set_time_zone("UTC")->strftime("%Y-%m-%d %T");
say join ",", @fields;
}
' <<END
1,aaaa,2018-11-03
2,bbbb,2018-02-03
END
答案 1 :(得分:0)
使用以下代码
工作正常#!/bin/bash
file=myfile
while IFS= read -r line
do
csid=$(echo $line | cut -f1 -d,)
name=$(echo $line | cut -f2 -d,)
oldmt=$(echo $line | cut -f3 -d,)
echo "$csid"",""$name"","$(date -u -d "TZ=\"MST7MDT\" $oldmt" "+%Y-%m-%d %H:%M:%S") >> newmyfile.dat done < $file