Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Currently I am running a command like this, to get the most requested content:

grep "17\/Jul\/2011" other_vhosts_access.log | awk '{print $8}' | sort | uniq -c | sort -nr

I want to now see the user agent strings, but the problem is they include several spaces. Here is a typical log file line. The UA is the last section delimited by quotation marks:

example.com:80 [ip] - - [17/Jul/2011:23:59:59 +0100] "GET [url] HTTP/1.1" 200 6449 "[referer]" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30"

Is there a better tool than awk for this?

share|improve this question

2 Answers 2

up vote 7 down vote accepted

If that format is consistent and the field is really wrapped in double quotes you can use either awk or cut with " as the field delimiter:

awk -F\" '{print $6}'

or:

cut -d\" -f 6
share|improve this answer
perl -ne 'if(/"([^"]+)"$/){$ua{$1}++;} END{for(keys %ua){print "$ua{$_} $_\n"}}' \
  access_log
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.