Click here to get back home

get quote enclosed field in a line

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
get quote enclosed field in a line xahlee@gmail.com 04-17-2008
Posted by xahlee@gmail.com on April 17, 2008, 7:06 pm
Please log in for more thread options
is there a simple way in perl, python, or awk/shell/pipe, that gets
the user agent field in a apache log?

e.g. the typical line is like this:

189.139.109.235 - - [07/Apr/2008:00:00:16 -0400] "GET /
Periodic_dosage_dir/lacru/manara.html HTTP/1.1" 200 1933 xahlee.org
"http://xahlee.org/Periodic_dosage_dir/lacru/manara2.html" "Mozilla/
5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.8.1.13) Gecko/20080311
Firefox/2.0.0.13" "-"

I want the part: "Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:
1.8.1.13) Gecko/20080311 Firefox/2.0.0.13".

Thanks.

Xah
xah@xahlee.org
=E2=88=91 http://xahlee.org/

=E2=98=84

Posted by Martin P. Hellwig on April 17, 2008, 7:21 pm
Please log in for more thread options
xahlee@gmail.com wrote:
> is there a simple way in perl, python, or awk/shell/pipe, that gets
> the user agent field in a apache log?
>
> e.g. the typical line is like this:
>
> 189.139.109.235 - - [07/Apr/2008:00:00:16 -0400] "GET /
> Periodic_dosage_dir/lacru/manara.html HTTP/1.1" 200 1933 xahlee.org
> "http://xahlee.org/Periodic_dosage_dir/lacru/manara2.html" "Mozilla/
> 5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.8.1.13) Gecko/20080311
> Firefox/2.0.0.13" "-"
>
> I want the part: "Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:
> 1.8.1.13) Gecko/20080311 Firefox/2.0.0.13".
>
> Thanks.
>
> Xah
> xah@xahlee.org
> ∑ http://xahlee.org/
>
> ☄

Something like:
# cut -d '"' -f 6 < httpd-access.log
?
--
mph

Posted by Skye Shaw!@#$ on April 18, 2008, 2:35 am
Please log in for more thread options
> xah...@gmail.com wrote:
> > is there a simple way in perl, python, or awk/shell/pipe, that gets
> > the user agent field in a apache log?

> Something like:
> # cut -d '"' -f 6 < httpd-access.log
> ?
> --
> mph

Doesn't it feel like autosplit mode never gets any run time?

perl -laF'"' -ne'print $F[5]' access_log


Posted by A. Sinan Unur on April 17, 2008, 7:39 pm
Please log in for more thread options

[ Replying only in clpm because I can only talk about Perl ]

> is there a simple way in perl

s/perl/Perl ;-)

> 189.139.109.235 - - [07/Apr/2008:00:00:16 -0400] "GET /
> Periodic_dosage_dir/lacru/manara.html HTTP/1.1" 200 1933
> xahlee.org
> "http://xahlee.org/Periodic_dosage_dir/lacru/manara2.html" "Mozilla/
> 5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.8.1.13) Gecko/20080311
> Firefox/2.0.0.13" "-"
>
> I want the part: "Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:
> 1.8.1.13) Gecko/20080311 Firefox/2.0.0.13".

Simple.

#!/usr/bin/perl

use strict;
use warnings;

my $line = <<EOL;
189.139.109.235 - - [07/Apr/2008:00:00:16 -0400] "GET /
Periodic_dosage_dir/lacru/manara.html HTTP/1.1" 200 1933 xahlee.org
"http://xahlee.org/Periodic_dosage_dir/lacru/manara2.html" "Mozilla/
5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.8.1.13) Gecko/20080311
Firefox/2.0.0.13" "-"
EOL

# deal with any extra spacing and/or
# line breaks inserted in posting

$line =~ s/^\s+//;
$line =~ s/\s+$//;
$line =~ s/\s+/ /g;


if ( $line =~ /"([^"]+)" "[^"]+"\z/ ) {
print "User agent: $1\n";
}

__END__

C:\Temp> lp
User agent: Mozilla/ 5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.8.1.13)
Gecko/20080311 Firefox/2.0.0.13

Sinan

--
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/

Posted by Michael Tosch on April 18, 2008, 1:17 pm
Please log in for more thread options
xahlee@gmail.com wrote:
> is there a simple way in perl, python, or awk/shell/pipe, that gets
> the user agent field in a apache log?
>
> e.g. the typical line is like this:
>
> 189.139.109.235 - - [07/Apr/2008:00:00:16 -0400] "GET /
> Periodic_dosage_dir/lacru/manara.html HTTP/1.1" 200 1933 xahlee.org
> "http://xahlee.org/Periodic_dosage_dir/lacru/manara2.html" "Mozilla/
> 5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.8.1.13) Gecko/20080311
> Firefox/2.0.0.13" "-"
>
> I want the part: "Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:
> 1.8.1.13) Gecko/20080311 Firefox/2.0.0.13".
>
> Thanks.
>
> Xah
> xah@xahlee.org
> ∑ http://xahlee.org/
>
> ☄

awk -F\" '{print $6}' httpd-access.log
awk -F\" 'NF>6{print $6}' httpd-access.log

--
Michael Tosch @ hp : com

Similar ThreadsPosted
How to handling string contains single quote and double quote April 28, 2007, 4:41 am
double quote to single quote April 19, 2005, 3:56 am
package in enclosed block March 17, 2006, 2:24 am
How to quote a semicolon? February 26, 2008, 3:12 am
FAQ: How can I quote a variable to use in a regex? October 23, 2004, 5:10 am
FAQ 6.8 How can I quote a variable to use in a regex? January 29, 2005, 6:03 pm
FAQ 6.8 How can I quote a variable to use in a regex? April 7, 2005, 11:03 pm
FAQ 6.8 How can I quote a variable to use in a regex? June 23, 2005, 5:03 pm
FAQ 6.8 How can I quote a variable to use in a regex? September 8, 2005, 10:03 pm
FAQ 6.8 How can I quote a variable to use in a regex? January 23, 2006, 12:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap