Dave's Brain

Browse - programming tips - bash get the first field

Date: 2009jul1
Language: awk, bash

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:

	1.2.3.4 - - [28/Jun/2009:01:09:51 -0400] "GET /abc/def.html HTTP/1.1" 304 - "-" "Firefox/1.0 (compatible; Win32; I)"

And give you:

	1.2.3.4

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.
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2010, 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.