The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk arrays imonthejazz Shell Programming and Scripting 1 09-21-2007 09:29 AM
arrays in awk??? craigsky Shell Programming and Scripting 3 08-27-2007 09:13 PM
ksh script - arrays sidamin810 Shell Programming and Scripting 13 07-18-2005 03:07 AM
KSH and arrays whited05 Shell Programming and Scripting 1 06-24-2005 12:07 PM
Two or more arrays in Awk nitin UNIX for Advanced & Expert Users 1 12-10-2001 09:37 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-10-2008
fusionX fusionX is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 9
Need Help with awk and arrays

now its owkring - thanks fo rthe help all .

Last edited by fusionX; 02-20-2008 at 01:03 AM.. Reason: changed.
  #2 (permalink)  
Old 02-10-2008
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
what exactly have you tried so far?
  #3 (permalink)  
Old 02-10-2008
fusionX fusionX is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 9
So far !

done - thanks for the help !

Last edited by fusionX; 02-20-2008 at 01:04 AM.. Reason: done.
  #4 (permalink)  
Old 02-11-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
Quote:
Originally Posted by fusionX View Post
Well, its a big script and i've done the other parts but i was stuck in this IP part, here's what tried doing for IP
Code:
{for (i=1;i<=NR;i++) numIP[$1]++}
This doesn't work as you expect. $1 stays the same. It's the same as doing:
Code:
 numIP[$1] = NR;
So each element in the array would be filled with the latest Record number. I'd expect the last IP address to have the highest count.

With all respect, I think you are confused about how AWK works. Your "program" is executed for each line of input, every time. It's like there's a big while loop around your code, and in each iteration, $0 is the input line, and $1, $2, etc, are the fields split via the regular expression in FS (whitespace by default). NR just indicates the current record (line) number. If you want to know if the current record's day is not the same as the previous, you have something like:

Code:

tmp=substr($4,2,2);
if (day != tmp) {
   # New day code here
   # ie, print how many IPs were hit on this day
   print hits_on_this_day;
   # change day to match current record
   day = tmp;
}
else {
  hits_on_this_day++;
}
No need to initialize "day".
  #5 (permalink)  
Old 02-10-2008
fusionX fusionX is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 9
check out what I had tried....
  #6 (permalink)  
Old 02-10-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
Arrays in awk (and php) are purely associative. So you can say

Code:
ip[$1]++;
Getting the busiest date and hour pretty much requires perl, but it's possible to do in gawk and maybe nawk. You'll need the mktime function in the least. It's ugly and you need to populate the full months table and some additional parsing.

Code:
BEGIN { m["Jan"]="01"; m["Feb"]="02"; } # and so on for all months
{ 

# split time field into numbers and letters
split($4,lt,"[^0-9a-zA-Z-]*"); 
# construct timestamp into internal unix representation
ts=mktime( lt[4] " " m[lt[3]] " " lt[2]  " " lt[5]  " " lt[6]  " " lt[7]  " " lt[8]); 
# you don't care about minutes and seconds, so just replace lt[7] and lt[6] with 0's. You could make *two* timestamps -- one for just the days (hours 0'd out) and another for just the hours (always Jan-1-1970, but with the hour filled in).

# bump count for this ip address
ip[$1]++; 
# 
day[ts]++;
}

END { 
  # find busiest day
  frequency=-1; busiest=-1;
  for (d in day) {
   if (day[d] > frequency) {
      frequency=day[d];
      busiest=d;
   }
  }
  print "busiest day: " busiest " hit " frequency " times";
  
}
  #7 (permalink)  
Old 02-10-2008
fusionX fusionX is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 9
Exclamation

la la la la la la - its owkring now.

Last edited by fusionX; 02-20-2008 at 01:04 AM.. Reason: working
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0