• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

How can use I awk to calculate the total number of records processed?

brianbills21

New Coder
Hi All,



I would like to add several lines to the output of my script. Currently, the script behaves as outlined in the README.md file here. But the behavior I want to change it to is below:

Code:
./sample.sh -s 0-9 -c 0-3 -t samples.bin
                Ch0     Ch1     Ch2     Ch3
Sample 0:       0x1a03  0x1a03  0x4a03  0x5703
Sample 1:       0x4b03  0x4403  0x1e03  0x0904
Sample 2:       0x1003  0x1903  0x4003  0xae03
Sample 3:       0x1e03  0x2603  0x3303  0xad03
Sample 4:       0x1003  0x8403  0x4303  0x6203
Sample 5:       0xe003  0x1603  0x3403  0xc403
Sample 6:       0xf802  0x3b03  0x5303  0x6103
Sample 7:       0x1003  0x1503  0x4203  0x5803
Sample 8:       0x2303  0x1f03  0x5703  0x6203
Sample 9:       0x1703  0x7303  0x3103  0x3303
Total Samples in Record:        374371
Total Samples Processed:        10

and here:

Code:
./sample.sh -r samples.bin
Total Samples in Record:        374371

and here:

Code:
./sample.sh -t samples.bin
Total Samples in Record:        374371
Total Samples Processed:        374371

The script works as expected, I just want the argument -r to replace -t and passing -t after -s and -c, should produce the total number of samples processed along with the total number of samples in the input file. I should still be able to pass -t to sample.sh, but in this case, it will count the number of samples in the file and tell us the total number of samples processed. The script is here. If you want you can clone the project and then do a pull request, and if the solution works, I'll approve it.





Thanks so much for your help,

Brian
 

Antero360

King Coder
Staff Team
Security Analyst
Hi All,



I would like to add several lines to the output of my script. Currently, the script behaves as outlined in the README.md file here. But the behavior I want to change it to is below:

Code:
./sample.sh -s 0-9 -c 0-3 -t samples.bin
                Ch0     Ch1     Ch2     Ch3
Sample 0:       0x1a03  0x1a03  0x4a03  0x5703
Sample 1:       0x4b03  0x4403  0x1e03  0x0904
Sample 2:       0x1003  0x1903  0x4003  0xae03
Sample 3:       0x1e03  0x2603  0x3303  0xad03
Sample 4:       0x1003  0x8403  0x4303  0x6203
Sample 5:       0xe003  0x1603  0x3403  0xc403
Sample 6:       0xf802  0x3b03  0x5303  0x6103
Sample 7:       0x1003  0x1503  0x4203  0x5803
Sample 8:       0x2303  0x1f03  0x5703  0x6203
Sample 9:       0x1703  0x7303  0x3103  0x3303
Total Samples in Record:        374371
Total Samples Processed:        10

and here:

Code:
./sample.sh -r samples.bin
Total Samples in Record:        374371

and here:

Code:
./sample.sh -t samples.bin
Total Samples in Record:        374371
Total Samples Processed:        374371

The script works as expected, I just want the argument -r to replace -t and passing -t after -s and -c, should produce the total number of samples processed along with the total number of samples in the input file. I should still be able to pass -t to sample.sh, but in this case, it will count the number of samples in the file and tell us the total number of samples processed. The script is here. If you want you can clone the project and then do a pull request, and if the solution works, I'll approve it.





Thanks so much for your help,

Brian
Hey there!

Does it have to be specifically awk? As an alternative, you could pipe the result of your script over to grep
resultOfScript | grep -c 'Sample[[:space:]][[:digit:]]'

-c will count the number of results, and that regex there, you are looking for the word 'Sample' followed by the space character followed by a digit
 

brianbills21

New Coder
I get what you mean @Antero360 and thank you very much for your solution. But the solution must come from within the script itself. I'm thinking about using a variable like iterations and give it the ++ so that every time the script loops for printf of the value of srange, it increments iterations. That's the solution I was hoping for.
 

brianbills21

New Coder
I found this on gnu.org:

The awk language has a for statement in addition to a while statement because a for loop is often both less work to type and more natural to think of. Counting the number of iterations is very common in loops. It can be easier to think of this counting as part of looping rather than as something to do inside the loop.


There is an alternative version of the for loop, for iterating over all the indices of an array:

Code:
for (i in array)
do something with array[i]

here.
 
Top