Pages

Wednesday, August 22, 2012

Log File Parsing using Shell Script

One of my colleague, just wanted me, to write a script to parse the log file
and get only certain words in a log file.

The log file format will be in the following format.

1337061923.319      0 218.248.7.53 TCP_DENIED/403 1484 GET http://www.baidu.com/ - NONE/- text/html
1337063859.637    393 192.168.37.2 TCP_MISS/200 507 POST http://mail.google.com/mail/channel/bind? - DIRECT/74.125.236.181 text/plain

Here the date in long data type.


He wanted the output in the following format.

DATE ,TIME ,WEB ADDREESS,..

I wrote the following script to get the required output.

I am sure that , this not an optimized script, how ever this could solve the
requirement.


#!/bin/bash
#Author: V. Srikrishnan

echo "Enter the  Input File Name:"
read file
echo "Enter the Output File Name:"
read output

echo "Processing..."
while read LINE
do
Time=`echo $LINE |awk {'print $1'}`
Time=`perl -e "print scalar localtime ($Time)"`
IP=`echo $LINE | awk {'print $3'}`
TYPE=`echo $LINE | awk {'print $6'}`
URL=`echo $LINE | awk {'print $7'}`
echo "$Time $IP $TYPE $URL" >> output
done < $file
echo "Completed.. Check the Output File, $output"




This script accepts 2 inputs
Input File
Output File.

No comments:

Post a Comment