不确定最有效的方法。 Bash似乎最简单,我在约会时有一个开始。
set -A mAMon N/A Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
sed -e 's/-/ /g' -e 's/\(.*:..\).\{4\}/\1/' -e 's/\.\([0-9]\)/ \1/g' -e 's/\./ /2' inp_file
我已经找到了如何提取列,但不知道如何转换
这就是我的......
代码:
NEWDNS 04-Jun-2011 06:00:59.762 10.220.136.217 crl.verisign.com
这就是我需要的....更改日期,删除密码秒,删除ips中的句点并删除网址中的最后一个点。
代码:
NEWDNS 2011-06-04 06:00:59 10 220 136 217 crl.verisign com
由于
答案 0 :(得分:2)
使用awk:
<强>更新强>
BEGIN {
split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", month, " ")
for (i=1; i<=12; i++) {
mdigit[month[i]] = sprintf("%02d", i)
}
}
{
#convert date
split($2, d, "-")
$2 = d[3] "-" d[2] "-" d[1]
sub(/[a-zA-Z]+/,mdigit[d[2]],$2)
# convert time
split($3, t, ".")
$3=t[1]
# ip
gsub(/\./, " ", $4)
#url
sub(/\./,"_", $5)
sub(/\./," ",$5)
sub(/_/,".",$5)
#glue everything together
print $1,$2,$3,$4,$5
}
的产率:
$ awk -f date.awk input
NEWDNS 2011-06-04 06:00:59 10 220 136 217 crl.verisign com
答案 1 :(得分:2)
一种perl方式:
my %months = (Jan=>1, Feb=>2, Mar=>3, Apr=>4, May=>5, Jun=>6, Jul=>7, Aug=>8, Sep=>9, Oct=>10, Nov=>11, Dec=>12);
while(<DATA>) {
my @part = split;
$part[1] =~ s/(\d+)-(\w+)-(\d+)/"$3-".sprintf('%02d',$months{$2})."-$1"/e;
$part[2] =~ s/\.\d+$//;
$part[3] =~ s/\./ /g;
$part[4] =~ s/\.(\w+)$/ $1/;
print "@part\n";
}
__DATA__
NEWDNS 04-Jun-2011 06:00:59.762 10.220.136.217 crl.verisign.com
<强>输出:强>
NEWDNS 2011-06-04 06:00:59 10 220 136 217 crl.verisign com
答案 2 :(得分:2)
PURE bash4方式:
declare -A mon=([Jan]=01 [Feb]=02 [Mar]=03 [Apr]=04 [May]=05 [Jun]=06 [Jul]=07 [Aug]=08 [Sep]=09 [Oct]=10 [Nov]=11 [Dec]=12)
while read txt date time ip host
do
IFS='-' read -ra xdate <<< "$date"
echo $txt ${xdate[2]}-${mon[${xdate[1]}]}-${xdate[0]} ${time%%.*} ${ip//./ } ${host%.*} ${host##*.}
done
所以,例如
declare -A mon=([Jan]=01 [Feb]=02 [Mar]=03 [Apr]=04 [May]=05 [Jun]=06 [Jul]=07 [Aug]=08 [Sep]=09 [Oct]=10 [Nov]=11 [Dec]=12)
while read txt date time ip host
do
IFS='-' read -ra xdate <<< "$date"
echo $txt ${xdate[2]}-${mon[${xdate[1]}]}-${xdate[0]} ${time%%.*} ${ip//./ } ${host%.*} ${host##*.}
done <<EOF
NEWDNS 04-Jun-2011 06:00:59.762 10.220.136.217 crl.verisign.com
NEWDNS 05-Jul-2012 07:00:59.862 11.220.136.217 crx.verisign.sm
EOF
将产生:
NEWDNS 2011-06-04 06:00:59 10 220 136 217 crl.verisign com
NEWDNS 2012-07-05 07:00:59 11 220 136 217 crx.verisign sm