LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-02-2004, 05:48 AM   #1
pippet
Member
 
Registered: May 2004
Posts: 67

Rep: Reputation: 15
want some help on a perl script


I got this script from squid-cache.org. This onverts a fieldin access log to a readable format.

cat access.log | perl -nwe 's/^(\d+)/localtime($1)/e; print' > test

i want to modify this script so that it also converts the second field which is the time elapsed in millyseconds to minutes or seconds. how can i do that? I don't know perl.
 
Old 08-02-2004, 06:24 AM   #2
Gnuru
Member
 
Registered: Jan 2004
Posts: 53

Rep: Reputation: 15
Anyway, this snippet of perl takes the first series of digits in a line and converts it to the ctime value. You didn't post the format of the logfile, however assuming after the first field there are spaces before the second field, subsitituting the regex for something like this might do the trick:

s/^(\d+)\s+(\d+)/localtime($1) . " " . $2\/1000/e

What do you mean by seconds and miliseconds by the way? Do you want it formatted like XX mins xx s?
 
Old 08-03-2004, 01:04 AM   #3
pippet
Member
 
Registered: May 2004
Posts: 67

Original Poster
Rep: Reputation: 15
i tried with a command line as below

cat access.log | perl -nwe 's/^(\d+)\s+(\d+)/localtime($1) ." ". $2\1000/e; print';.


I got the following error

Can't use \1 to mean $1 in expression at -e line 1.
Backslash found where operator expected at -e line 1, near "$2\"
(Missing operator before \?)
syntax error at -e line 1, near "$2\"
Execution of -e aborted due to compilation errors.
./view_log2: line 1: .: filename argument required
.: usage: . filename

You may want to see the format of access.log
a single line is like this.

1090827471.120 2205 192.168.0.32 TCP_MISS/200 24692 GET http://mail.yahoo.com/? - DIRECT/216.109.127.60 text/html


The first field is the time of request in Unix format second field is the time elapsed. the two fields are to be converted.
 
Old 08-03-2004, 02:58 AM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
try :
cat access.log |  perl -p -nwe 's/^(\d+\.\d+)\s+(\d+)\s.*$/localtime($1)." ".$2\/1000/e'

or :
cat access.log |  perl -p -nwe 's/^(\d+\.\d+)\s+(\d+)\s(.*$)/localtime($1)." ".($2\/1000)." ".($3)/e'

Last edited by Cedrik; 08-03-2004 at 03:10 AM.
 
Old 08-03-2004, 04:07 AM   #5
pippet
Member
 
Registered: May 2004
Posts: 67

Original Poster
Rep: Reputation: 15
I have tried it out, The first one gives the below o/p
Mon Aug 2 09:06:00 2004 0.705
The second one
Sun Aug 1 11:28:23 2004 1.038 192.168.0.32 TCP_MISS/200 197 GET http://toolbarqueries.google.com/search? - DIRECT/64.233.161.99 text/html

Thanks a lot.
Will u please help me to modify the Ist commandline sothat it also includes the third field too?
 
Old 08-03-2004, 04:09 AM   #6
pippet
Member
 
Registered: May 2004
Posts: 67

Original Poster
Rep: Reputation: 15
Another question,Can u suggest a good tutorial for perl?
 
Old 08-03-2004, 06:24 AM   #7
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
For me the third field is the IP, what do you call third field ?

For my part, I learnt Perl on the net and with practice on the command line and scripts, so I suggest you to practice

[edit]
I think I understand what you want :
Code:
cat access.log |  perl -p -nwe 's/^(\d+\.\d+)\s+(\d+)\s(\d+\.\d+\.\d+\.\d+)\s(.*$)/localtime($1)." ".($2\/1000)." ".($3)/e'

Last edited by Cedrik; 08-03-2004 at 06:29 AM.
 
Old 08-04-2004, 11:58 PM   #8
pippet
Member
 
Registered: May 2004
Posts: 67

Original Poster
Rep: Reputation: 15
u r correct. the third field is ip. I want to trace the requests to a particular site
Your script will do that.
Thanks alot!
 
Old 08-05-2004, 01:30 AM   #9
pippet
Member
 
Registered: May 2004
Posts: 67

Original Poster
Rep: Reputation: 15
I have another question to ask.

I have to keep seperate log files for each PC. I put the script u have given in a file named view_log_minimal


#file view_log_minimal
grep "websitename" /var/log/squid/access.log | perl -p -nwe 's/^(\d+\.\d+)\s+(\d+)\s(\d+\.\d+\.\d+\.\d+)\s(.*$)/localtime($1)." ".($2\/1000)." ".($3)/e'

i wrote another shell script to get ip address wise output

#file ip_wise_list

#!/bin/bash

X=0
while [ $X -le 60 ]
do
X=$((X+1))
IPADR=$(printf "192.168.0.%d" $X)
./view_log_minimal | grep $IPADR
done


There is some problem. It takes more than one second to process each ip. It shows the result .

Can u tell me what is the problem with my script?

If u have an alternative with perl, i will be happy to implement that
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
please help .perl Script/ apenguinlinux Programming 4 08-10-2005 09:04 PM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM
perl script... killjoy Programming 0 03-29-2001 03:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration