awk multiply fields within lines, then total


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk multiply fields within lines, then total
# 1  
Old 09-05-2011
awk multiply fields within lines, then total

Here is my scenario, I have two columns. I need to add the first columns (the easy part). The second column is a percent that I need to subtract by 100 then multiply against the first column for each line, then sum the output for a total

Here is the input:

Code:
942330863 96
942172150 95
942099452 92

I need this as an output:

Code:
2826602465 160169797

I tried this code, but it's just doing the percentage work and only doing it for the first line and not summing every line.

Code:
awk '
BEGIN {FS=","}                                                   
    NR == 1{ n1 = $1; x = substr((100-$2)/100,2,3); y = x*$1;  next }
    { n1 += $1; y += $y }
END { printf ("%-15d%d\n",n1,y) 
}'


Last edited by radoulov; 09-05-2011 at 05:49 PM.. Reason: Code tags!
# 2  
Old 09-05-2011
Code:
 awk 'END { printf "%d %d\n", s, p }
{ s += $1; p += (100 - $2) * $1 / 100 }
  ' infile


Last edited by radoulov; 09-05-2011 at 06:57 PM.. Reason: Corrected :)
# 3  
Old 09-05-2011
Code:
# cat file
942330863 96
942172150 95
942099452 92
# awk 'END{printf "%d %d\n",a,b}{_=10^2;a+=$1;b+=($1/_)*(_-$2)}' file
2826602465 160169798

# 4  
Old 09-05-2011
Thanks all!! I modified it just a little to multiply by a percentage. I added a printf so the large number would be printed instead of the exponential notation.

Code:
awk 'END { printf "%-15d%d\n", s, p } { s += $1; p += (100 - $2) * 0.01 * $1 }'

However, I just through some more numbers at the infile:

Code:
942330863 96
942172150 95
942099452 92
943387548 91

Gets me a result of:
Code:
3769990013     245074677

But more info my infile truncates the total in column 1:

Code:
942330863 96
942172150 95
942099452 92
943387548 91
941335230 89

result:
Code:
4.71133e+09    348621552


Last edited by radoulov; 09-05-2011 at 06:58 PM.. Reason: Code tags, please!
# 5  
Old 09-05-2011
base on last sample

Code:
# awk 'END{printf "%d %d\n",a,b}{_=10^2;a+=$1;b+=($1/_)*(_-$2)}' file
3769990013 245074677

# awk --version
GNU Awk 3.1.3
Copyright (C) 1989, 1991-2003 Free Software Foundation.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine lines if fields match in lines

In the awk below, what I am attempting to do is check each line in the tab-delimeted input, which has ~20 lines in it, for a keyword SVTYPE=Fusion. If the keyword is found I am splitting $3 using the . (dot) and reading the portion before and after the dot in an array a. If it does have that... (12 Replies)
Discussion started by: cmccabe
12 Replies

2. Shell Programming and Scripting

awk to remove lines where field count is greather than 1 in two fields

I am trying to remove all the lines and spaces where the count in $4 or $5 is greater than 1 (more than 1 letter). The file and the output are tab-delimited. Thank you :). file X 5811530 . G C NLGN4X 17 10544696 . GA G MYH3 9 96439004 . C ... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Finding out the common lines in two files using 4 fields with the help of awk and UNIX

Dear All, I have 2 files. If field 1, 2, 4 and 5 matches in both file1 and file2, I want to print the whole line of file1 and file2 one after another in my output file. File1: sc2/80 20 . A T 86 F=5;U=4 sc2/60 55 . G T ... (1 Reply)
Discussion started by: NamS
1 Replies

4. Shell Programming and Scripting

awk summing specific lines and fields

Hi I would like to know if it is possible to sum some specific fields. I have this x;x;x;x;x;x;x;x;467,390,611 Bytes;0.435291 GB;0.062247 GB;0.373045 GB;11,225;157 a;a;a;a;a;a;a;a;13,805,156,846 Bytes;12.857054 GB;1.838559 GB;11.018495 GB;151,063;18,933 b;b;b;b;b;b;b;b;232,797,478,723... (5 Replies)
Discussion started by: nakaedu
5 Replies

5. Shell Programming and Scripting

select lines with certain values on certain fields with awk

I need a awk command to select from a log-file only the lines that have on the 2nd field (considering "|" separator) one of the values 10.216.22.XX or 10.216.22.YY or 10.216.22.ZZ and on the 4th field only values that contain strictly digits. I want the command to work parsing the file only once (I... (2 Replies)
Discussion started by: black_fender
2 Replies

6. Shell Programming and Scripting

awk split lines without knowing the number of fields a-priori

I want to use awk to split fields and put them into a file but I don't know the number of fields for example, in the following line Ports: 22/filtered/tcp//ssh///, 53/open/tcp//tcpwrapped///, 111/filtered/tcp//rpcbind///, 543/filtered/tcp//klogin///, 544/filtered/tcp//kshell///,... (3 Replies)
Discussion started by: esolvepolito
3 Replies

7. Shell Programming and Scripting

AWK swapping fields on different lines

Hi All, Sorry if this question has been posted elsewhere, but I'm hoping someone can help me! Bit of an AWK newbie here, but I'm learning (slowly!) I'm trying to cobble a script together that will save me time (is there any other kind?), to swap two fields (one containing whitespace), with... (5 Replies)
Discussion started by: Bravestarr
5 Replies

8. Shell Programming and Scripting

awk - use fields from subsequent lines

I've run into a problem getting exactly what I want out of awk - some folks may recognize this as an output from Amazon's ec2-describe-instances: Given the following: INSTANCE i-4960f321 BLOCKDEVICE Line2Var2 TAG instance i-4960f321 Name web1 TAG instance i-4960f321... (2 Replies)
Discussion started by: colinjohnson
2 Replies

9. Shell Programming and Scripting

AWK : Add Fields of lines with matching field

Dear All, I would like to add values of a field, if the lines match in a certain field. Then I would like to divide the sum though the number of lines that have a matched field. This is the Input: Input: Test1 5 Test1 10 Test2 2 Test2 5 Test2 13 Test3 4 Output: Test1 7.5 Test1 7.5... (6 Replies)
Discussion started by: DerSeb
6 Replies

10. Shell Programming and Scripting

How to (n)awk lines of CSV with certain number of fields?

I have a CSV file with a variable number of fields per record. How do I print lines of a certain number of fields only? Several permutations of the following (including the use of escape characters) have failed to retrieve the line I'm after (1,2,3,4)... $ cat myfile 1,2,3,4 1,2,3 $ # Print... (1 Reply)
Discussion started by: cs03dmj
1 Replies
Login or Register to Ask a Question