get quote enclosed field in a line

X

xahlee

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
(e-mail address removed)
∑ http://xahlee.org/

☄
 
M

Martin P. Hellwig

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
(e-mail address removed)
∑ http://xahlee.org/

☄

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

John Machin

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".

C:\junk>type xah.py
import cStringIO, csv, pprint
line = '''189.139.109.235 - etc etc etc'''
f = cStringIO.StringIO(line)
reader = csv.reader(f, delimiter=" ")
row = reader.next()
pprint.pprint(row)

C:\junk>xah.py
['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 Fi
refox/2.0.0.13',
'-']

If you don't like that, just hang about -- there's sure to be a
pyparsing bus coming by real soon now :)

Cheers,
John
 
C

Carsten Haese

If you don't like that, just hang about -- there's sure to be a
pyparsing bus coming by real soon now :)

While we're waiting for the pyparsing bus, here's the shlex train:

import shlex
line = """blah blah blah"""
print shlex.split(line)[10]

HTH,
 
P

Paul McGuire

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".

C:\junk>type xah.py
import cStringIO, csv, pprint
line = '''189.139.109.235 - etc etc etc'''
f = cStringIO.StringIO(line)
reader = csv.reader(f, delimiter=" ")
row = reader.next()
pprint.pprint(row)

C:\junk>xah.py
['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 Fi
refox/2.0.0.13',
  '-']

If you don't like that, just hang about -- there's sure to be a
pyparsing bus coming by real soon now :)

Cheers,
John

Beep, beep!

You can find a pyparsing-based log server file parser at
http://pyparsing.wikispaces.com/space/showimage/httpServerLogParser.py.

Vrooom!
-- Paul
 
S

Skye Shaw!@#$

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

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

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

Michael Tosch

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
(e-mail address removed)
∑ http://xahlee.org/

☄

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top