LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 04-26-2020, 09:05 AM   #1
Drosera_capensis
Member
 
Registered: Jun 2018
Posts: 153

Rep: Reputation: Disabled
make a sum in the awk command


Hello everyone,

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.
 
Old 04-26-2020, 09:25 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
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.

Code:
awk    'NR==FNR { sum=sum+$1; next }
        NR!=FNR { print $1,$1/sum }' \
        OFS="\t" \
        file file
So in this case cat must really be avoided.
 
1 members found this post helpful.
Old 04-26-2020, 09:50 AM   #3
Drosera_capensis
Member
 
Registered: Jun 2018
Posts: 153

Original Poster
Rep: Reputation: Disabled
It is working, thanks Turbocapitalist. I have to spend more time studying the awk syntax.

I am a bit confused by the "file file" at the end of the command, but I suppose it is for each part of the awk command?
 
Old 04-26-2020, 09:59 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
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.
 
Old 04-27-2020, 04:52 AM   #5
Drosera_capensis
Member
 
Registered: Jun 2018
Posts: 153

Original Poster
Rep: Reputation: Disabled
Thanks for the recommandations. I have one more question about the output format given by awk.

The former command print output in scientifict format (9.50368e-05). I wish to output results in plain numbers.

I have used printf to do so, but it outputs a unique string of results, without spaces or jump to next row.

I have tried the following syntax, with %s. , but it does not work.
Quote:
awk 'NR==FNR { sum=sum+$1; next }
NR!=FNR { printf %s.$1/sum }' \
OFS="\t" \
file file
Would anyone know if the printf formating allow to jump to next row?
Or if any command can suppress the scientific format of the output given by awk?
 
Old 04-27-2020, 04:55 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by Drosera_capensis View Post
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.

Code:
man awk
 
Old 04-27-2020, 05:43 AM   #7
Drosera_capensis
Member
 
Registered: Jun 2018
Posts: 153

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-27-2020, 05:55 AM   #8
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by Drosera_capensis View Post
{printf output, "\n"}
For printf, it should be in reverse order:
Code:
{printf "%s\n", output}
 
Old 04-29-2020, 03:42 PM   #9
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
With miller:
Code:
seq 50 |
 mlr --csv -N put -q '
   @sum+=$1;@num[NR]=$1;
   end{
     for (k,v in @num){print v."\t".v/@sum}
   }'

Last edited by shruggy; 04-29-2020 at 05:06 PM.
 
Old 01-31-2022, 12:34 PM   #10
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
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)
Code:
mlr --n2t -N fraction -f 1
mshare from the MCMD toolset is another possibility.
Code:
KG_VerboseLevel=1 mshare -nfn -x -q f=0

Last edited by shruggy; 02-01-2022 at 02:49 AM.
 
  


Reply

Tags
awk



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Carry out an awk command to sum a list of numbers but include 0's nwalsh88 Linux - Newbie 4 12-05-2012 06:44 AM
[SOLVED] How to sum bytes, kilobytes, megabytes and gigabytes using awk command roopakl Linux - Newbie 8 11-30-2012 12:02 PM
awk's sum not working? grob115 Linux - Newbie 4 04-29-2010 12:44 PM
[SOLVED] awk - sum total if field = string schneidz Programming 12 03-20-2010 04:56 PM
awk merge and sum lines problem lalo4080 Programming 4 08-12-2008 10:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration