loop through file of numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting loop through file of numbers
# 1  
Old 08-14-2005
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  
Old 08-14-2005
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  
Old 08-15-2005
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  
Old 08-15-2005
Thanks for your response vgersh99

Could you give me an example of your two mentioned files:
fileWITHranges.txt
fileWITHdata.txt
# 5  
Old 08-15-2005
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  
Old 08-15-2005
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  
Old 08-15-2005
try nawk instead of awk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

2. Shell Programming and Scripting

Sum up numbers in a for loop

Hi i have to calculate some numbers, column by column. Herfore i used a for-loop.. for i in {4..26};do awk -F"," '{x'$i'+=$'$i'}END{print '$i'"\t" x'$i'}' file.tmp;done ----- printout ----- 4 660905240 5 71205272 6 8.26169e+07 7 8.85961e+07 8 8.60936e+07 9 7.42238e+07 10 5.6051e+07... (7 Replies)
Discussion started by: IMPe
7 Replies

3. Shell Programming and Scripting

Reset while loop to loop same file multiple times

Hi, I want to read file multiple times. Right now i am using while loop but that is not working. ex. While read line do while read line2 do echo stmt1 #processing some data based on data., done < file2.txt done < file1.txt # This will have 10... (4 Replies)
Discussion started by: tmalik79
4 Replies

4. Shell Programming and Scripting

Help with Using "while" loop to output series of numbers

Trying to use "while" loop command to create a series of numbers that looks like the following: 0 . 1 0 . 2 1 0 . 3 2 1 0 . 4 3 2 1 0 . 5 4 3 2 1 0 . 6 5 4 3 2 1 0 . 7 6 5 4 3 2 1 0 . 8 7 6 5 4 3 2 1 0 . 9 8 7 6 5 4 3 2 1 0 . I am very new to shell scripting and any help would be... (7 Replies)
Discussion started by: tarlz
7 Replies

5. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

6. Shell Programming and Scripting

A way to store 2 random numbers from a for loop?

I have a for loop that cycles twice and generates 1 random number for each pass through. I would like to be able to store the two numbers to use later for arithmetics. Is there a way to do that? Right now I can only seem to use the last random number for anything. Thanks. (4 Replies)
Discussion started by: AxlVanDamme
4 Replies

7. Shell Programming and Scripting

Extract rows from file based on row numbers stored in another file

Hi All, I have a file which is like this: rows.dat 1 2 3 4 5 6 3 4 5 6 7 8 7 8 9 0 4 3 2 3 4 5 6 7 1 2 3 4 5 6 I have another file with numbers like these (numbers.txt): 1 3 4 5 I want to read numbers.txt file line by line. The extract the row from rows.dat based on the... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

8. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

9. Shell Programming and Scripting

Loop assistance, getting array of random numbers and feeding to a command, how-to?

Hi all, I need a little assistance to complete the following script. I would like to take a file with a single number on each line and for each number, run it through a command. The loop will terminate once all numbers have been checked. Here is what I have thus far... COUNTER=`wc -l... (2 Replies)
Discussion started by: boolean2222
2 Replies

10. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies
Login or Register to Ask a Question