Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
I would like to make the ratio of all the 50 numbers that are in a column of a file. So, the ratio version of a column of numbers.
The ratio would be equal to row value / sum of all the column.
Code:
for f in {1..50};
do
cat file | awk ' {sum += $1} {a = $1; b = sum; print (a/b)}' ;
done
But the result is zero at every loop. WOuld anyone know what is wrong with this command?
Thanks for the help!
Last edited by Drosera_capensis; 04-26-2020 at 09:21 AM.
You'd have to read the file to know the sum of the column. So that means you'd have to make two passes through the file. You can compare the NR and FNR to see if you are on the "first" file or the "second" file. Since these are the same file, it is just whether you are on the first pass through it or not.
AWK can read an arbitrary number of files. Here the same file is fed in twice.
Both instructions are read for each file, but only the first file has NR and FNR matching. Only the second and subsequent files will have NR and FNR not match. Take a quick look in the manual page for AWK to see what those built-in variables stand for. There are some others which are often handy.
I have tried the following syntax, with %s. , but it does not work.
Re-check the syntax for AWK's printf() in the reference manual. It is rather similar to printf() as found elsewhere and quite different from what you wrote.
I have read the BSD Library Functions Manual on printf(3) (link given by man awk). I have to admit I don't understand most of it. I have made some experiments with it.
Quote:
{print output, "\n"}
Is outputing results jumping a line, as I wished.
Quote:
{printf output, "\n"}
Is not changing the output of printf. A long string of output.
Could anyone give me some clues about this curious result?
Last edited by Drosera_capensis; 04-27-2020 at 05:47 AM.
Revisiting the old thread to give a simpler Miller solution (the more so as the solution outlined above won't work unchanged in Miller 6, see issue #909)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.