tags:

views:

87

answers:

3

Hello everyone. I have a log file containing some Whois entries with relative IP addresses which I want to censor like: 81.190.123.123 in 81.190.xxx.xxx.

Is there a way to make such a conversion and rewrite the file contents without modifying the rest?

Thank you for the help!

A: 

If Python is not actually one of your requirements, this also solves the problem:

sed -i 's/\([0-9]\{1,3\}\)\.\([0-9]\{1,3\}\)\.[0-9]\{1,3\}\.[0-9]\{1,3\}/\1.\2.xxx.xxx/g' mylogfile.log

Or Perl, which lets you get rid of most of the ugly backslashes:

perl -i -pe 's/(\d{1,3})\.(\d{1,3})\.\d{1,3}\.\d{1,3}/$1.$2.xxx.xxx/g' mylogfile.log

But this does not have the "inline" flag -i.

Thomas
Perl does indeed offer a `-i` flag.
Greg Hewgill
Of course it does. If you can think of it, Perl has it. I should've known.
Thomas
+2  A: 

As mentioned above, you can do this with sed:

sed -E -e 's/([0-9]+\.[0-9]+)\.[0-9]+\.[0-9]+/\1.xxx.xxx/g'

This uses a regular expression match to look for IP addresses and replace the last two octets with xxx. Using the -i switch, you can do this all at once:

sed -i.bak -E -e 's/([0-9]+\.[0-9]+)\.[0-9]+\.[0-9]+/\1.xxx.xxx/g' file.txt
Greg Hewgill
These solutions works great. The only point is that on the same line i have another string like 192.168.xxx.xxx | ... and this substitution messes up the space indent.Is there another way with sed too to replace multiple space chars (not single space char bot more than one, like " ") with a "\t"?
Julio
@Julio: Sure, just add that to the regex and replacement text.
Greg Hewgill
A: 

If you do want to use Python then use the fileinput module to process a file or files line by line.

import fileinput
for line in fileinput.input(["filename"], inplace=1, backup='.bak'):
    print processed(line)
fileinput.close()

fileinput with the inplace=1 will rename the input file and read from the renamed file while directing stdout to a new file of the same name. You can use the backup parameter to prevent the temporary file being deleted automatically.

If the input is important you'll need to take care to handle exceptions to prevent the input being lost in case of an error.

Duncan