mrlayance
mrlayance

Reputation: 663

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.verisign.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

Upvotes: 1

Views: 261

Answers (3)

clt60
clt60

Reputation: 63892

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

Upvotes: 2

Toto
Toto

Reputation: 91375

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

Upvotes: 2

Fredrik Pihl
Fredrik Pihl

Reputation: 45634

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

Upvotes: 2

Related Questions