The UNIX and Linux Forums  


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
How to replace many numbers with one number in a file vpandey AIX 2 02-27-2008 08:43 AM
to replace one character by numbers in a file cdfd123 Shell Programming and Scripting 7 10-07-2007 11:25 PM
comparing numbers in a file dusk2dawn UNIX for Dummies Questions & Answers 7 09-23-2007 03:54 AM
how to remove files with only numbers as file names? praveen_indramo Shell Programming and Scripting 6 06-07-2007 10:13 AM
removing all numbers from file rinku Shell Programming and Scripting 2 05-24-2007 09:33 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-14-2005
frustrated1 frustrated1 is offline
Registered User
  
 

Join Date: Aug 2003
Location: Ireland
Posts: 278
loop through file of numbers

Hope I can explain this correctly..

Have 1 file - filea containing something like the following

00012Bobsmyth
00065JohnDoe
00024MaryOwen

I have a range defined in separate functions and basically are set as follows:
customer typea 00001-00020
customer typeb 00021-00040
customer typec 00041-00100

How can I loop through the file to find how many customer type a I have/ how many customer type b I have and how many customer type c I have??
Obviously the input file is a lot bigger than the example above.
  #2 (permalink)  
Old 08-14-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
awk -f frust.awk fileWITHranges.txt fileWITHdata.txt

here's frust.awk:

Code:
     # BLOCK 1
     # FNR==NR is true when dealing with the FIRST file - file with ranges
FNR==NR {
      #    save the customer name from the SECOND field in the array 'cust' indexed
      #    by the record/line number
  cust[FNR]=$2
      #    split the LAST [$NF] field [range specification] by '-' and save the results
      #    in an array 't'
  split($NF, t, "-")
      #    save the LOW limit of the range 't[1]' in an array 'custLow' indexed by 
      #    currect record/line number
  custLow[FNR]=t[1]
      #    do the same as above for the HIGH limit in the range
  custHigh[FNR]=t[2]

      #    save the currect record/line number in a variable
  maxType=FNR
      #    jump to the NEXT record/line 
  next
}

      #    BLOCK 2
      #    Here we're dealing with the OTHER file specified on the command line
      #    fileWITHdata.txt - line by line.
{
      #    locate the pattern in a line - from the beginning of the line [^] followed
      #    by one or more digits - [0-9][0-9]*
  match($0, /^[0-9][0-9]*/)
      #    extract the sub-string from the line [$0] starting at position '1' for  the
      #    'RLENGTH' number of characters - 'RLENGTH' gets set by the previous
      #    'match' function when the pattern was searched/matched - the return
      #    value is the NUMBER.
  num=substr($0, 1, RLENGTH)

      #    iterate from '1' to the maxType - 'maxType' was the number of 
      #    lines/records processed in 'BLOCK 1' - number of 'range' specifications.
  for(i=1; i <= maxType; i++) {
       #    determine if the current number 'num' falls within one of the range arrays
       #    populated in 'BLOCK 1'
    if ( num >= custLow[i] && num <= custHigh[i] )
          #    If it does, increment the number the value in the array 'final' which is
          #    indexed by the cutomerName - the csutomerName was saved in 'BLOCK 1'
          #    and was indexed by the record/line number which is our iterator in this
          #    surrounding 'for' loop.
      final[cust[i]]++
  }
}

         #   END of processing block - this gets executed when ALL the files have been
         #   processed
END {
         #   iterate through ALL the indecies 'i' in array 'final' - 'i' contains the name of
         #   a customer. The value of a given array cell is the NUMBER of times a 
         #   particular customer 'fell' within the specified range(s). Print out the
         #   results.
  for (i in final)
    printf("%s -> [%d]\n", i, final[i])
}


Last edited by vgersh99; 08-15-2005 at 09:54 AM..
  #3 (permalink)  
Old 08-15-2005
futurelet futurelet is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 137

Code:
ruby -ne "BEGIN{$a=[0]*3};$a[[($_.to_i-1)/20,2].min]+=1;END{puts $a}" data

With this input:

00012Bobsmyth
00065JohnDoe
00024MaryOwen
00099VinceViar
00100WesWeber
00040MelMartin

the output is

1
2
3
  #4 (permalink)  
Old 08-15-2005
frustrated1 frustrated1 is offline
Registered User
  
 

Join Date: Aug 2003
Location: Ireland
Posts: 278
Thanks for your response vgersh99

Could you give me an example of your two mentioned files:
fileWITHranges.txt
fileWITHdata.txt
  #5 (permalink)  
Old 08-15-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
frustWITHdata.txt

Code:
00012Bobsmyth
00065JohnDoe
00024MaryOwen
00099VinceViar
00100WesWeber
00040MelMartin

fileWITHranges.txt

Code:
customer typea 00001-00020
customer typeb 00021-00040
customer typec 00041-00100

  #6 (permalink)  
Old 08-15-2005
frustrated1 frustrated1 is offline
Registered User
  
 

Join Date: Aug 2003
Location: Ireland
Posts: 278
Tried this - error below:

awk -f frust.awk fileWITHranges.txt fileWITHdata.txt
awk: syntax error near line 12
awk: illegal statement near line 12
  #7 (permalink)  
Old 08-15-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
try nawk instead of awk.
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 12:27 PM.


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