python - Howto 16bit left logical shift all ip addresses in CSV file (octets 3&4 become 1&2) -


i'd 16 bit logical left shift on ip address in csv file (thosands of rows)

example

(octets 3&4 become 1&2)

172.22.200.5 -> 200.5.0.0   (octect #1 [172] , octect #2 [22] shifted away) 10.1.1.0 -> 1.0.0.0         (octect #1 [10]  , octect #2 [1] shifted away) 

each row have ip address in field #4 , #5

2014-01-24 12:10:39.760,760,0.000,10.1.40.27,10.1.40.2,17,40424,514,502000,2000,......,0

before

2014-01-24 12:10:39.760,760,0.000,10.1.40.27,10.1.40.2,17,40424,514,502000,2000,......,0 2014-01-24 12:12:07.760,760,0.000,172.14.40.37,1.127.40.2,17,57278,514,558000,2000,......,0 2014-01-24 12:12:42.760,760,0.000,10.1.40.250,10.1.40.2,17,42347,514,500000,2000,......,0 2014-01-24 12:12:07.760,760,0.000,1.168.40.37,192.13.40.2,17,57278,514,558000,2000,......,0 

after

2014-01-24 12:10:39.760,760,0.000,40.27.0.0,40.2.0.0,17,40424,514,502000,2000,......,0 2014-01-24 12:12:07.760,760,0.000,40.37.0.0,40.2.0.0,17,57278,514,558000,2000,......,0 2014-01-24 12:12:42.760,760,0.000,40.250.0.0,40.2.0.0,17,42347,514,500000,2000,......,0 2014-01-24 12:12:07.760,760,0.000,40.37.0.0,40.2.0.0,17,57278,514,558000,2000,......,0 

i processing csv file on linux , fine bash, sed, awk, perl, python, etc. resolve issue.

any ideas?

thanks!

an ipv4 address 32-bit number. each part represents 1 of bytes. 16-bit shift two-byte shift, a.b.c.d becomes c.d.0.0. such, need is

$ip =~ s/^\d+\.\d+\.(\d+\.\d+)\z/$1.0.0/; 

you use real csv parse correct field, or can fudge it.

s/^([^,]*,[^,]*,[^,]*,)\d+\.\d+\.(\d+\.\d+),\d+\.\d+\.(\d+\.\d+)/$1$2.0.0$3.0.0/; 

it looks away with

s/,\d+\.\d+\.(\d+\.\d+)(?=,)/,$1.0.0/g; 

as one-liner:

perl -pe's/,\d+\.\d+\.(\d+\.\d+)(?=,)/,$1.0.0/g' 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -