From owner-freebsd-questions Fri Jan 4 13:25: 1 2002 Delivered-To: freebsd-questions@freebsd.org Received: from harrier.prod.itd.earthlink.net (harrier.mail.pas.earthlink.net [207.217.120.12]) by hub.freebsd.org (Postfix) with ESMTP id A76B437B41F for ; Fri, 4 Jan 2002 13:24:49 -0800 (PST) Received: from sdn-ar-008dcwashp222.dialsprint.net ([63.178.91.238] helo=moo.holy.cow) by harrier.prod.itd.earthlink.net with esmtp (Exim 3.33 #1) id 16MbpU-0006PY-00 for freebsd-questions@freebsd.org; Fri, 04 Jan 2002 13:24:45 -0800 Received: by moo.holy.cow (Postfix, from userid 1001) id B377E50E56; Fri, 4 Jan 2002 15:27:51 -0500 (EST) Date: Fri, 4 Jan 2002 15:27:51 -0500 From: parv To: f-q Subject: any way to make efficient the awk script Message-ID: <20020104202751.GA82783@moo.holy.cow> Mail-Followup-To: f-q Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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