How to change my script to another so that all of it is done via awk?
This code works correctly to sum column $2 and $3 where $1=IP:
Code:
while read IP |
I have already told you: http://www.linuxquestions.org/questi...9/#post5163847.
Have you tried that solution? |
Quote:
|
|
Quote:
It is an example of my DayReport-May-04 192.168.40.29 0% 1265.756 192.168.40.1 0% 693.506 192.168.40.4 10% 1112.107 192.168.40.1 10% 754.015 192.168.40.4 0% 635.516 192.168.40.29 0% 538.657 192.168.40.22 0% 640.583 192.168.40.4 0% 615.745 192.168.40.1 0% 507.429 ... -------------------------------------------------------------------------------- and I have some IP addresses in IPvar that is read line by line via while loop: 192.168.40.1 192.168.40.27 192.168.40.5 192.168.40.29 ... --------------------------------------------------------------------------------- I need to sum of amounts of column 2 and 3 for each IP, for example, the output for above files should be: 192.168.40.1 20% 1954.95 ... 192.168.40.29 0% 1804.413 --------------------------------------------------------------------------------- I've tried this code via awk: Code:
while read IP |
/IP/ means awk will take IP as string, not as variable, therefore it will look for the text IP in your input file.
you can try the following instead: Code:
awk -v IP="$IP" '$1 == IP {sumloss += $2;timeDelay += $3} END {print $1,"\t",sumloss,"\t",timeDelay}' |
Simply read in the file with all of the addresses in it and then cycle through the second file creating your totals. You will then need to utilise the 'END' action within awk to display the data in the format you require
|
This is a case where I would replace some logic with, say, "a very small Perl script." Or, in this case, with "awk itself (you don't need a separate script at all)."
"You have some rather funky magic going on" in the present (convoluted ...) script, and, although it might (or might not) be argued that "well, it works ...", such things are so hard to maintain (and, as you now see, so hard to debug!) that I routinely replace them, and/or ask a member of my team to do so. (Yes, I take my turn at "sweeping out the garbage," when I'm not wearing my management-hat and thus sweeping in more garbage. ;) ) You see, what this script in-aggregate does is to read a file of "IP-addresses and other things," and to extract certain variables out of it to produce certain results. But does it, cleanly and with just one tool, do that? Uhh, no. It uses two tools clumsily piped together, and neither of the two bits of source-code provided to either of the two tools completely expresses the total of what you actually want to accomplish. First of all, a short (but true) awk script could quite-effortlessly do this: it just requires, well, "a separate file instead of a one-liner." You simply invoke the awk command giving it the name of the IP-address file and the name of a short but separate awk script. All of the necessary logic now sits within that script; not in whatever invokes awk. Perl is awk's big-brother, so to speak, in that it literally was conceived as "an extension of awk" in its early days and still bears noticeable resemblances to it. Aptly called "the Swiss ArmyŽ Knife of data processing," it's a tool that I use quite often. But once again, never as a one-liner in production settings. |
Code:
grep -Ff IPvar DayReport-May-04 | awk '{ |
Quote:
|
Quote:
|
to #10: yes, sure, did you put it into that while cycle from port #5? (you just need to replace the line containing the awk command).
to #11: probably you can simply drop that grep and use only the awk by ntubski |
Quote:
|
All times are GMT -5. The time now is 12:36 AM. |