loop through file of numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting loop through file of numbers
# 8  
Old 08-15-2005
Perfect - works great

Could I ask that you explain the code used to me a little..
I know some scripting but am not familar with what you have used below and would help me play around a bit more before I have to post for help here.
# 9  
Old 08-15-2005
050815125630,000120,1817067361,3,B,0.00,0,1003
050815125635,000545,1817067379,3,B,0.00,0,901
050815125637,000687,1817067381,3,B,0.00,0,3906
050815125639,000401,1817067386,3,B,0.00,0,919


My input file is similar to the above - how do I change the match to find the number above and segment?
First field is date - the second field it the cust id..

Sorry for all the questions but i really dont know awk/nawk well
# 10  
Old 08-15-2005
Quote:
Originally Posted by frustrated1
Perfect - works great

Could I ask that you explain the code used to me a little..
I know some scripting but am not familar with what you have used below and would help me play around a bit more before I have to post for help here.
updated the code with comments - see original posting.
# 11  
Old 08-15-2005
Thats a great explanation.. I update the awk script as follows:
num=substr($0, 14, RLENGTH)

Now it works for me perfectly...
Thanks again for your help and explanation - it has helped me a lot.
# 12  
Old 08-15-2005
Quote:
Originally Posted by frustrated1
050815125630,000120,1817067361,3,B,0.00,0,1003
050815125635,000545,1817067379,3,B,0.00,0,901
050815125637,000687,1817067381,3,B,0.00,0,3906
050815125639,000401,1817067386,3,B,0.00,0,919


My input file is similar to the above - how do I change the match to find the number above and segment?
First field is date - the second field it the cust id..

Sorry for all the questions but i really dont know awk/nawk well
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
}

FNR == 1 { FS=","; $0=$0 }

      #    BLOCK 2
      #    Here we're dealing with the OTHER file specified on the command line
      #    fileWITHdata.txt - line by line.
{
  printf("\tDEBUG:: num->[%s]\n", $2)
      #    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 ( $2 >= custLow[i] && $2 <= 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 11:30 AM..
# 13  
Old 08-15-2005
Quote:
Originally Posted by frustrated1
Thats a great explanation.. I update the awk script as follows:
num=substr($0, 14, RLENGTH)

Now it works for me perfectly...
Thanks again for your help and explanation - it has helped me a lot.
This is assuming the length of the first first - maybe a safe assumption, but nevertheless...

Use the alternate solution posted above with the a different FS [','] for your data file....
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