Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 4 Jan 2002 15:27:51 -0500
From:      parv <parv_@yahoo.com>
To:        f-q <freebsd-questions@freebsd.org>
Subject:   any way to make efficient the awk script
Message-ID:  <20020104202751.GA82783@moo.holy.cow>

next in thread | raw e-mail | index | archive | help
hi,

below is my first awk script.  is there any way to make it any more
efficient?  in particular, i want to know if printf() & print
statements could be combined.

goal is to shorten each line of apache http log for quick
browsing/sorting.

- begin ---

#! /bin/sh

logs="/path/to/log/file(s)"

#  given a line this (from apache log)...
#
#------------------------------------------------------------------------------------------
# 1        2 3   4                     5      6    7          8       9    10   11    12..$
#------------------------------------------------------------------------------------------
#127.0.0.1 - - [30/Dec/2001:14:43:28 -0500] "GET /file/path HTTP/1.0" 200 57853 "-" "User ... Agent"
#
# ...awk gives..
#
#   w/o seconds (implemented)
#127.0.0.1   2001.12.30 14.43 -0500  /file/path  (200)  -
#
#   w/ seconds (not implemented)
#127.0.0.1   2001.12.30 14.43.28 -0500  /file/path  (200)  -
#


awk '
BEGIN \
{  # create month_num array to map month names to numbers

  month_name_list = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
  split(month_name_list, month_names, ",")

  for (i in month_names)
  {
    month = month_names[i]
    month_num[month] = sprintf("%0.2d", i)
  }
}
! /w3c_validator.*libwww-perl|baa.*sheep|moo|\.css|robots\.txt|GET \/ HTTP/ \
{
  # create date array containing: day, month(name), year, hour, minute, second
  split(substr($4,2,20), date, "/|:")

  printf("%-18s",$1)  # left justify ip4 address w/ 3 extra spaces at the end

  #     year    .  month number      .  day      hour   .   minute   timezone offset
  #      |           |                   |        |          |         |

  print date[3]"."month_num[date[2]]"."date[1], date[4]"."date[5], substr($5,1,length($5)-1), \
  " ", $7, "  ("$9")  ", substr($11,2,length($11)-2)

  #    |        |              |
  #  file     (http code)    referrer

}' $logs

- end ---

-- 
 

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020104202751.GA82783>