Sponsored Content
Full Discussion: How to run it in the loop??
Top Forums Shell Programming and Scripting How to run it in the loop?? Post 302774961 by Don Cragun on Monday 4th of March 2013 12:31:43 AM
Old 03-04-2013
You originally indicated that you wanted column totals as well as row totals. And you haven't said if the number of columns is a constant for a given input file. The following is considerably more complex than bipinajith's suggestion, but allows the number of input columns to vary from line to line and prints column totals as well as row totals. (Note that the row totals you showed in message #3 in this thread do not match the data in the columns in those rows.)

The awk script:
Code:
awk 'BEGIN {FS = OFS = ","}
{       # Update mf if there are more fields in this line than in previous lines.
        if(NF > mf) mf = NF
        # Save data from the 1st 4 fields on the line.
        for(i = 1; i < 5; i++) d[NR, i] = $i
        # Calculate sum of fields 5 and later on this line, save data, and keep
        # running column totals.
        for(i = 5; i <= NF; i++) {
                R[NR] += d[NR, i] = $i
                c[i] += $i
        }
        # Keep running total for sum of fields 5 and later for all lines.
        T += R[NR]
}
END {   # Print data and sum for each input row.
        for(i = 1; i <= NR; i++) {
                for(j = 1; j <= mf; j++)
                        printf("%s%s", d[i,j], OFS)
                printf("%d\n", R[i])
        }
        # Print totals for columns 5 and later and the grand total.
        printf("total,,,,")
        for(j = 5; j <= mf; j++)
                printf("%d%s", c[j], OFS)
        printf("%d\n", T)
}' FULLNFINAL_VONE_X

With the input you gave in message #3, it produces the output:
Code:
ALE,APESH,id1_0,COMBO   ,4      ,9      ,28     ,8      ,13     ,11     ,14     ,13     ,17,117
ALE,APESH,id2_1_*,COMBO ,0      ,0      ,4      ,3      ,1      ,1      ,2      ,2      ,0,13
ALE,APESH,id2_2,COMBO   ,41     ,25     ,13     ,34     ,40     ,14     ,47     ,18     ,6,238
ALE,APESH,id2_3,COMBO   ,54     ,36     ,53     ,52     ,49     ,28     ,34     ,31     ,1,338
ALE,APESH,id2_5,COMBO   ,0      ,0      ,0      ,0      ,0      ,0      ,0      ,0      ,0,0
ALE,APESH,id2_7,COMBO   ,0      ,1      ,1      ,0      ,0      ,1      ,0      ,0      ,0,3
ALE,APESH,id3_2_*,COMBO ,7      ,6      ,4      ,4      ,2      ,4      ,3      ,3      ,2,35
ALE,APESH,id3_3,COMBO   ,56     ,67     ,43     ,36     ,32     ,57     ,54     ,39     ,0,384
ALE,APESH,id4_2,COMBO   ,1      ,0      ,1      ,2      ,0      ,1      ,0      ,0      ,0,5
ALE,APESH,id4_3,COMBO   ,1      ,0      ,0      ,0      ,1      ,0      ,0      ,0      ,1,3
total,,,,164,144,147,139,138,117,154,106,27,1136

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

The loop does not run as expected

Hi folks, I have the following configuration file: DB_LAYER=NO ADMIN_LAYER=NO RTESUB_LAYER=NO DB_HOST_NAME=tornado ADMIN_HOST_NAME=tornado RTESUB_HOST_NAME=tornado RESPONSE_FILE_SR=/tmp/SR.rsp INSTALL_SR_1=/home/Upgrade_4.7.1/Utilities/Install_SR:Y... (8 Replies)
Discussion started by: nir_s
8 Replies

2. Shell Programming and Scripting

Is there a better way I could have run this loop. (For loop with two variables)

Sorry for such a dreadful title, but I'm not sure how to be more descriptive. I'm hoping some of the more gurutastic out there can take a look at a solution I came up with to a problem, and advice if there are better ways to have gone about it. To make a long story short around 20K pieces of... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

3. Shell Programming and Scripting

Script to run infinite loop

Hi all, I have a script which triggers batch admin manager and gets the top 10 jobs and their status info. the output of this script is the list of all these jobs. I want to run this in infinite loop which will show top 100 jobs' status. the script is as follows #!/bin/sh exec &> capture1.txt... (1 Reply)
Discussion started by: digitalrg
1 Replies

4. Shell Programming and Scripting

How To Run A For Loop In A Remsh?

Hi all, I'm trying to remsh to another server and then execute a for loop command there but I'm getting unexpected errors and would appreciate any suggestions. Ideally what I want to do is this: for host in `cat host_file` do remsh $host -n " cd /home/ for DATABASE in `ls -d... (5 Replies)
Discussion started by: Korn0474
5 Replies

5. Shell Programming and Scripting

How to run the following expect commands in a loop

#!/usr/bin/expect spawn telnet 1.1.1.1 expect login* send “admin\r” expect Password* send “abcdef123\r” expect “Router#” send “exit\r” I want the above code to run in a loop such that script keeps doing a telnet to the device. Please suggest Tarun (1 Reply)
Discussion started by: tkhanna82
1 Replies

6. Shell Programming and Scripting

How do I run a shell command in a while loop?

The command is: sic -h irc.freenode.net 2>&1 | tee -a irc.log Where sic is an IRC client, and I'm piping the output to tee in order to log my IRC sessions. I'm trying to handle reconnects by running it in a while loop in the shell process and cat the initial commands into sic's stdin. I... (1 Reply)
Discussion started by: guitarscn
1 Replies

7. Shell Programming and Scripting

Script for telnet and run one command kill it and run another command using while loop

( sleep 3 echo ${LOGIN} sleep 2 echo ${PSWD} sleep 2 while read line do echo "$line" PID=$? sleep 2 kill -9 $PID done < temp sleep 5 echo "exit" ) | telnet ${HOST} while is executing only command and exits. (5 Replies)
Discussion started by: sooda
5 Replies

8. Shell Programming and Scripting

Run the for loop in parallel

I have the below code which runs on multiple databases , but this runs one-after-one. I will need this to run in parallel so that i could save a lot of time. Please help!!! Thanks in advance for Db in `cat /var/opt/oracle/oratab |egrep -v "ASM" |grep -v \# |cut -d\: -f1` do { export... (5 Replies)
Discussion started by: jjoy
5 Replies

9. Shell Programming and Scripting

Trying to run a basic for loop

OS : RHEL 6.1 Shell : Bash I had a similair post on this a few weeks back. But I didn't explain my requirements clearly then. Hence starting a new thread now. I have lots of files in /tmp/stage directory as show below. I want to loop through each files to run a command on each file. I... (8 Replies)
Discussion started by: kraljic
8 Replies

10. Shell Programming and Scripting

Get vaule and from file and need to run in loop

Hi All, I have a command which provide this output symaccess -sid 624 show PG_E36_PG6P -type port |grep FA FA-5G:1 FA-6G:0 FA-11G:0 FA-12G:1 I need to use the value in loop like this (5 Replies)
Discussion started by: ranjancom2000
5 Replies
SHELL-QUOTE(1p) 					User Contributed Perl Documentation					   SHELL-QUOTE(1p)

NAME
shell-quote - quote arguments for safe use, unmodified in a shell command SYNOPSIS
shell-quote [switch]... arg... DESCRIPTION
shell-quote lets you pass arbitrary strings through the shell so that they won't be changed by the shell. This lets you process commands or files with embedded white space or shell globbing characters safely. Here are a few examples. EXAMPLES
ssh preserving args When running a remote command with ssh, ssh doesn't preserve the separate arguments it receives. It just joins them with spaces and passes them to "$SHELL -c". This doesn't work as intended: ssh host touch 'hi there' # fails It creates 2 files, hi and there. Instead, do this: cmd=`shell-quote touch 'hi there'` ssh host "$cmd" This gives you just 1 file, hi there. process find output It's not ordinarily possible to process an arbitrary list of files output by find with a shell script. Anything you put in $IFS to split up the output could legitimately be in a file's name. Here's how you can do it using shell-quote: eval set -- `find -type f -print0 | xargs -0 shell-quote --` debug shell scripts shell-quote is better than echo for debugging shell scripts. debug() { [ -z "$debug" ] || shell-quote "debug:" "$@" } With echo you can't tell the difference between "debug 'foo bar'" and "debug foo bar", but with shell-quote you can. save a command for later shell-quote can be used to build up a shell command to run later. Say you want the user to be able to give you switches for a command you're going to run. If you don't want the switches to be re-evaluated by the shell (which is usually a good idea, else there are things the user can't pass through), you can do something like this: user_switches= while [ $# != 0 ] do case x$1 in x--pass-through) [ $# -gt 1 ] || die "need an argument for $1" user_switches="$user_switches "`shell-quote -- "$2"` shift;; # process other switches esac shift done # later eval "shell-quote some-command $user_switches my args" OPTIONS
--debug Turn debugging on. --help Show the usage message and die. --version Show the version number and exit. AVAILABILITY
The code is licensed under the GNU GPL. Check http://www.argon.org/~roderick/ or CPAN for updated versions. AUTHOR
Roderick Schertler <roderick@argon.org> perl v5.8.4 2005-05-03 SHELL-QUOTE(1p)
All times are GMT -4. The time now is 02:22 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy