Processing a log to fix a malformed IP address ?.?.?.x

Posted by skymook on Stack Overflow See other posts from Stack Overflow or by skymook
Published on 2010-03-08T15:50:32Z Indexed on 2010/03/09 0:51 UTC
Read the original article Hit count: 433

I would like to replace the first character 'x' with the number '7' on every line of a log file using a shell script. Example of the log file:

216.129.119.x [01/Mar/2010:00:25:20 +0100] "GET /etc/....
74.131.77.x [01/Mar/2010:00:25:37 +0100] "GET /etc/....
222.168.17.x [01/Mar/2010:00:27:10 +0100] "GET /etc/....

My humble beginnings...

#!/bin/bash
echo Starting script...
cd /Users/me/logs/
gzip -d /Users/me/logs/access.log.gz
echo Files unzipped...
echo I'm totally lost here to process the log file and save it back to hd...

exit 0

Why is the log file IP malformed like this? My web provider (1and1) has decide not to store IP address, so they have replaced the last number with the character 'x'. They told me it was a new requirement by 'law'. I personally think that is bs, but that would take us off topic.

I want to process these log files with AWstats, so I need an IP address that is not malformed. I want to replace the x with a 7, like so:

216.129.119.7 [01/Mar/2010:00:25:20 +0100] "GET /etc/....
74.131.77.7 [01/Mar/2010:00:25:37 +0100] "GET /etc/....
222.168.17.7 [01/Mar/2010:00:27:10 +0100] "GET /etc/....

Not perfect I know, but least I can process the files, and I can still gain a lot of useful information like country, number of visitors, etc. The log files are 200MB each, so I thought that a shell script is the way to go because I can do that rapidly on my Macbook Pro locally. Unfortunately, I know very little about shell scripting, and my javascript skills are not going to cut it this time. I appreciate your help.

© Stack Overflow or respective owner

Related posts about bash

Related posts about shell-scripting