开发者

Converting columns of a text file

Not sure the most effient way to do this. Bash seems the easiest, I have a start on the date.

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

I have figiured out how to extract columns, but not sure how to convert

This is what I have...

Code:

NEWDNS 04-Jun-2011 06:00:59.762 10.220.136.217 crl.ve开发者_JAVA技巧risign.com

This is what I need.... Change date, remove mil seconds, remove periods in the ips and remove the last dot in a url.

Code:

NEWDNS 2011-06-04 06:00:59 10 220 136 217 crl.verisign com

Thanks


A perl way to do it:

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

output:

NEWDNS 2011-06-04 06:00:59 10 220 136 217 crl.verisign com


Using awk:

Updated

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
}

yields:

$ awk -f date.awk input
NEWDNS 2011-06-04 06:00:59 10 220 136 217 crl.verisign com


PURE bash4 way:

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

so, for example

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

will produce:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜