summary line help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting summary line help
# 1  
Old 09-06-2008
summary line help

Code:
$ cat file
a:12:56:12
b:23:12:23
d:32:24:12
c:90:12:24

required output:

a:12:56:12
b:23:12:23
d:32:24:12
c:90:12:24
t:157:104:71

t line is the total line, which will be the last line in the output.

Please help.

I tried this:

$ awk 'BEGIN {FS=OFS=":"}{sum+=$2} {sum1+=$3} {sum2+=$4} END {print "t",sum,sum1,sum2}' file
t:157:104:71

How can perform the same using a for loop in awk, please help.

# 2  
Old 09-06-2008
Code:
{ for (i=2; i<=NF; ++i) sum[i] += $i; j=NF }
END { printf "%s", t; for (i=2; i <= j; ++i) printf "%s%s", OFS, sum[i]; printf "\n"; }

There's probably a more elegant way ...
# 3  
Old 09-07-2008
Quote:
Originally Posted by uwork72
I tried this:
Code:
$ awk 'BEGIN {FS=OFS=":"}{sum+=$2} {sum1+=$3} {sum2+=$4} END {print "t",sum,sum1,sum2}' file
t:157:104:71

Print the lines first.
Code:
awk 'BEGIN {FS=OFS=":"}{print; sum+=$2; sum1+=$3; sum2+=$4} END {print "t",sum,sum1,sum2}' file

# 4  
Old 09-07-2008
Another one in awk::

Code:
awk -F":" '{ print; } END { while((getline < "filename")>0) { var1+=$2;var2+=$3;var3+=$4; } print "t:"var1":"var2":"var3; }' filename


Last edited by dennis.jacob; 09-07-2008 at 02:41 AM..
# 5  
Old 09-07-2008
Quote:
Originally Posted by dennis.jacob
Another one in awk::

Code:
awk -F":" '{ print; } END { while((getline < "filename")>0) { var1+=$2;var2+=$3;var3+=$4; } print "t:"var1":"var2":"var3; }' filename

Do you really want to parse the file a second time?
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Subroutine or Function Summary

I have a fortran file with code declarations such as Subroutine str_tnum_tu & ( & s, dl, tu, pos & ) ! Class (*), Intent (InOut) :: tu(:) Character (Len=*), Intent (In) :: s, dl Character (Len=*), Intent (In), Optional :: pos ... or ... (11 Replies)
Discussion started by: kristinu
11 Replies

2. Shell Programming and Scripting

Generate a Summary report

Hi All, Script to meet my requirement might be simpler for UINIX experts.. :) I need to generate an summary report in .txt file using shell script I have Reject directory in Unix server which contains all reject files for three diff categories- Presentation, Chapter and Scene Following... (3 Replies)
Discussion started by: Sakthikalluri
3 Replies

3. Shell Programming and Scripting

Summary using awk

Displaying information using awk Hey guys, i am using awk to display my information in a certain order. Database : Persia:42:John France:50:Mabel Persia:50:Rach Germany:60:JohnMy expected output is : ... (25 Replies)
Discussion started by: gregarion
25 Replies

4. UNIX for Dummies Questions & Answers

Email to show only summary line

Hi everyone, can you suggest a way fro me to set something up so that i can start mail or mailx and have only the summary line of each email displayed, 24 lines (emails) per page and keep on navigating down throught the summary lines a full page at a time? My users have hundreds of emails ....... (0 Replies)
Discussion started by: gio001
0 Replies

5. UNIX for Dummies Questions & Answers

Directory Summary Command

Is there a unix command to provides the number of files in a directory? (2 Replies)
Discussion started by: rbarlow
2 Replies

6. UNIX for Dummies Questions & Answers

System Summary Tools

Are there any system sumary tools for Linux? What are some good ones for Solaris and HP-Ux? (2 Replies)
Discussion started by: vader
2 Replies
Login or Register to Ask a Question