How to change my script to another so that all of it is done via awk?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
--------------------------------------------------------------------------------
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
do
set -vx
awk -v IP="$IP" '/IP/{sumloss += $2;timeDelay += $3} END {print $1,"\t",sumloss,"\t",timeDelay}' ./PingReportFolder/DayReport-May-04
done < IPvar
It just work for the last record of DayReport-May-04!!!!
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 inmore 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.
Last edited by sundialsvcs; 05-05-2014 at 07:35 AM.
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.