Speaking of IPv4: you can create a list of sed commands from your hosts file:
sed -rn 's/^(([0-9]{1,3}\.){3}([0-9]{1,3}))[ \t]([^ \t]+)[ \t].*/s#\1#\4#/p' /etc/hosts > hosts.sed
Then apply it in your log file:
sed -f hosts.sed LOGFILE
Of course, your hostnames must be listed in the host file.
Another, reverse approach would be to use logresolve .
From the man page:
NAME logresolve - Resolve IP-addresses to hostnames in Apache log files SYNOPSIS logresolve [ -s filename ] [ -c ] < access_log > access_log.new SUMMARY logresolve is a post-processing program to resolve IP-addresses in Apache access logfiles. To minimize impact on your nameserver, logresolve has its very own internal hash-table cache. This means that each IP number will only be looked up the first time it is found in the log file. Takes an Apache log file on standard input. The IP addresses must be the first thing on each line and must be separated from the remainder of the line by a space.
So, you can use REGEX to extract all IP addresses, put them 2 times in a new file, once in the first column and convert it using logresolve. Then use this table to create such a sedfile as above.
source share