AWK: add and sum line in output


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers AWK: add and sum line in output
# 1  
Old 08-27-2012
AWK: add and sum line in output

Hi,
I have this output:

extended device statistics
Code:
device    r/s    w/s   kr/s   kw/s wait actv  svc_t  %w  %b
sd1       0.0    0.0    0.0    0.0  0.0  0.0    0.0   0   0
sd2       0.1    2.9    2.6   24.8  0.0  0.1   21.0   0   1
sd3       0.1    2.9    2.7   24.8  0.0  0.1   21.1   0   1
sd4       0.0    0.0    0.0    0.0  0.0  0.0    0.2   0   0
sd5       0.0    0.0    0.0    0.0  0.0  0.0    0.1   0   0

I would like same output but with addition of one line with name ONE in column "device" with sum of the remainder column for the line sd1,sd2 and sd3 and another line with name TWO in same column "device" with sum of column in line sd4 and sd5.
Is it possibile?
thanks
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 08-27-2012 at 12:54 PM.. Reason: code tags, please!
# 2  
Old 08-27-2012
Well, this would do the job, but formatting needs to be refined as the original formatting is lost:
Code:
awk     'BEGIN {f[1,1]="ONE"; f[2,1]="TWO"; OFS="\t"}
        /sd[123]/ {for (i=2;i<NF;i++) f[1,i]+=$i}
        /sd[45]/  {for (i=2;i<NF;i++) f[2,i]+=$i}
        $1=$1
        END {for (i=1;i<=2;i++) {printf "%s ",f[i,1]; for (j=2;j<=10;j++) printf "\t%3.1f", f[i,j]; print ""}}
        ' infile

yielding
Code:
extended    device    statistics
device    r/s    w/s    kr/s    kw/s    wait    actv    svc_t    %w    %b
sd1       0.0    0.0    0.0     0.0     0.0     0.0     0.0      0     0
sd2       0.1    2.9    2.6     24.8    0.0     0.1     21.0     0     1
sd3       0.1    2.9    2.7     24.8    0.0     0.1     21.1     0     1
sd4       0.0    0.0    0.0     0.0     0.0     0.0     0.2      0     0
sd5       0.0    0.0    0.0     0.0     0.0     0.0     0.1      0     0
ONE       0.2    5.8    5.3     49.6    0.0     0.2     42.1     0.0   0.0
TWO       0.0    0.0    0.0     0.0     0.0     0.0     0.3      0.0   0.0

# 3  
Old 08-28-2012
thank you very much for your answer SmilieSmilieSmilie, the format is not a problem.
When I have in "device" column over number 10 awk sum with sd1 and same with number 100.
For example:

Code:
awk     'BEGIN {f[1,1]="ONE"; f[2,1]="TWO"; OFS="\t"}
        /sd[123]/ {for (i=2;i<NF;i++) f[1,i]+=$i}
        /sd[45]/  {for (i=2;i<NF;i++) f[2,i]+=$i}
        $1=$1
        END {for (i=1;i<=2;i++) {printf "%s ",f[i,1]; for (j=2;j<=10;j++) printf "\t%3.1f", f[i,j]; print ""}}
        '

"sd3" name has been interpreted with "sd30"

Code:
device  r/s     w/s     kr/s    kw/s    wait    actv    svc_t   %w      %b
sd1     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0       0
sd2     0.1     2.9     2.6     24.8    0.0     0.1     21.0    0       1
sd30    0.1     2.9     2.7     24.8    0.0     0.1     21.1    0       1
sd4     0.0     0.0     0.0     0.0     0.0     0.0     0.2     0       0
sd5     0.0     0.0     0.0     0.0     0.0     0.0     0.1     0       0
ONE     0.2     5.8     5.3     49.6    0.0     0.2     42.1    0.0     0.0
TWO     0.0     0.0     0.0     0.0     0.0     0.0     0.3     0.0     0.0

Smilie
# 4  
Old 08-28-2012
Code:
awk     'BEGIN {f[1,1]="ONE"; f[2,1]="TWO"; OFS="\t"}
        $1 ~ /^sd[123]$/ {for (i=2;i<NF;i++) f[1,i]+=$i}
        $1 !~ /^sd[123]$/  {for (i=2;i<NF;i++) f[2,i]+=$i}
        $1=$1
        END {for (i=1;i<=2;i++) {printf "%s ",f[i,1]; for (j=2;j<=10;j++) printf "\t%3.1f", f[i,j]; print ""}}
        '

# 5  
Old 08-28-2012
Yes, it evaluates just ONE digit after "sd". That's what you requested, no numerical interpretation of the device no. If the requirement has changed, pls specify exactly what you need, e.g. "anything above 3 and below 100 into TWO"
# 6  
Old 08-28-2012
Yes, best solution is a calculation, for example from line 3 at line 16 in TWO and from line 20 at line 23 in ONE, because I have same numeric in output with "iostat"
# 7  
Old 08-28-2012
Sorry - I don't understand. Best would be to supply input sample including extreme values and desired output sample. Even better were you train your awk capabilities and publish your own result.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk printing output to new line

Hi I have a file profile.txt with the below input: {"atgUserId":"736f14c4-eda2-4531-9d40-9de4d6d1fb0f","firstName":"donna","lastName":"biehler","email":"schoolathome42@live.com","receiveEmail":"y es"}, {"atgUserId":"c3716baf-9bf8-42da-8a44-a13fff68d20f","firstName":"Gilberto... (6 Replies)
Discussion started by: ankur328
6 Replies

2. Shell Programming and Scripting

Print awk output in same line ,For loop

My code is something like below. #/bin/bash for i in `ps -ef | grep pmon | grep -v bash | grep -v grep | grep -v perl | grep -v asm | grep -v MGMT|awk '{print $1" "$8}'` do echo $i ORACLE_SID=`echo $line | awk '{print $2}'` USERNAME=`echo $line | awk '{print $1}'` done ============= But... (3 Replies)
Discussion started by: tapia
3 Replies

3. Shell Programming and Scripting

Output on one line using awk or sed

I have a file of 100,000 lines in the below format: answer.bed chr1 957570 957852 NOC2L chr1 976034 976270 PERM1 chr1 976542 976787 PERM1 I need to get each on one line and so far what I have tried doesn't seem to be working. Thank you... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. UNIX for Dummies Questions & Answers

awk output line by line

Hi all, New to the forum and somewhat of a Bash newbie so go easy. ;) I have some data in a tsv file, a sample of which looks like this (ignore what look like duplicates, in reality there are many more columns of data): SAMD11 NM_152486.2 intronic SAMD11 NM_152486.2 intronic... (2 Replies)
Discussion started by: winkleman
2 Replies

5. Shell Programming and Scripting

Reading ls -l output line by line awk the user name and su user to run commands

Using ksh on AIX what I am trying to do is to read the ls -l output from a file in a do while loop line by line. Extract the user name(3rd field) and the directory/file name(9th field) using awk and save them into variables. su -c to the user and change directory/file permisions to 777. Script I... (13 Replies)
Discussion started by: zubairom
13 Replies

6. Shell Programming and Scripting

How to sum multiple column output with awk ?

Hi Experts, I am trying to sum multiple columns and rows with awk , I want the sum of : 1] Horizontal Sum: (rows sum): 2] Vertical Sum: (Column's sum] details: # cat file1 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 40 31 32 33 34 35 36 37 38 39 70 41 42 43 44... (2 Replies)
Discussion started by: rveri
2 Replies

7. Shell Programming and Scripting

Need to have output of AWK array in one line

I have this code echo $logfile | awk ' {arr++; next} END { for (i in arr) {print i} }' that gives me this output result1 result2 result3 I try to figure out how to get it like this result1 result2 result3 (4 Replies)
Discussion started by: Jotne
4 Replies

8. Shell Programming and Scripting

[SOLVED] Awk one line to sum up a function

I need help with debugging an error in my awk script. I have a shell script with variable named U_new_i and want to pass it to awk for use in a summation. The original file have the following content. cat test.txt -2445.7132000000 -2444.9349000000 -2444.3295000000 -2443.1814000000 ... (0 Replies)
Discussion started by: Quantum_Dot
0 Replies

9. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

10. Shell Programming and Scripting

awk help required to group output and print a part of group line and original line

Hi, Need awk help to group and print lines to format the output as shown below INPUT FORMAT set echo on set heading on set spool on /* SCHEMA1 */ CREATE TABLE T1; /* SCHEMA1 */ CREATE TABLE T2; /* SCHEMA1 */ CREATE TABLE T3; /* SCHEMA1 */ CREATE TABLE T4; /* SCHEMA1 */ CREATE TABLE T5;... (5 Replies)
Discussion started by: rajan_san
5 Replies
Login or Register to Ask a Question