Dave's Brain

Browse - Programming Tips - How can I get the first (space-delimited) field?

Date: 2009jul1
Language: awk, bash
OS: Linux

Q.  How can I get the first (space-delimited) field?

A.  The awk command/language is great for this.
For example:

awk '{print $1}' /var/log/httpd/access_log
will take a line like this from the Apache log: - - [28/Jun/2009:01:09:51 -0400] "GET /abc/def.html HTTP/1.1" 304 - "-" "Firefox/1.0 (compatible; Win32; I)"
And give you:
You can pipe the result to sort:
awk '{print $1}' /var/log/httpd/access_log | sort -u
to find all the unique IP-addresses recently.

Add a comment

Sign in to add a comment
Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.