![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Summary Explorer Output | gryffindor | SUN Solaris | 3 | 05-15-2008 09:08 AM |
| How to summary one command's cpu usage? | jiarong.lu | HP-UX | 1 | 02-22-2008 11:03 AM |
| Email to show only summary line | gio001 | UNIX for Dummies Questions & Answers | 0 | 11-20-2007 02:32 PM |
| Directory Summary Command | rbarlow | UNIX for Dummies Questions & Answers | 2 | 03-01-2005 02:24 PM |
| System Summary Tools | vader | UNIX for Dummies Questions & Answers | 2 | 07-03-2002 03:28 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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.
|
|
||||
|
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 ... |
|
||||
|
Quote:
Code:
awk 'BEGIN {FS=OFS=":"}{print; sum+=$2; sum1+=$3; sum2+=$4} END {print "t",sum,sum1,sum2}' file
|
|
||||
|
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.. |
|
||||
|
Do you really want to parse the file a second time?
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|