How to run it in the loop??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to run it in the loop??
# 1  
Old 03-04-2013
How to run it in the loop??

I have this code

Code:
Code:
awk -F, '                                                                                                                                          {
    C5+=$5
    C6+=$6
    C7+=$7
    C8+=$8
    R=$5+$6+$7+$8
    T+=R
  }
  {
    print $0,R
  }
  END {
    print "total,,,, " C5,C6,C7,C8,T
  }
' OFS=, FULLNFINAL_VONE_X

Issue is its givin me the total from 5 to 8 columns

but my file's columns are volatile... it mite have columns from 5 to 30...

to be clear the columns are volatile..

I wanted this code to be modified
# 2  
Old 03-04-2013
Can you also post sample content from your input file & desired output?
# 3  
Old 03-04-2013
Code:
Code:
ALE,APESH,id1_0,COMBO   ,4      ,9      ,28     ,8      ,13     ,11     ,14     ,13     ,17
ALE,APESH,id2_1_*,COMBO ,0      ,0      ,4      ,3      ,1      ,1      ,2      ,2      ,0
ALE,APESH,id2_2,COMBO   ,41     ,25     ,13     ,34     ,40     ,14     ,47     ,18     ,6
ALE,APESH,id2_3,COMBO   ,54     ,36     ,53     ,52     ,49     ,28     ,34     ,31     ,1
ALE,APESH,id2_5,COMBO   ,0      ,0      ,0      ,0      ,0      ,0      ,0      ,0      ,0
ALE,APESH,id2_7,COMBO   ,0      ,1      ,1      ,0      ,0      ,1      ,0      ,0      ,0
ALE,APESH,id3_2_*,COMBO ,7      ,6      ,4      ,4      ,2      ,4      ,3      ,3      ,2
ALE,APESH,id3_3,COMBO   ,56     ,67     ,43     ,36     ,32     ,57     ,54     ,39     ,0
ALE,APESH,id4_2,COMBO   ,1      ,0      ,1      ,2      ,0      ,1      ,0      ,0      ,0
ALE,APESH,id4_3,COMBO   ,1      ,0      ,0      ,0      ,1      ,0      ,0      ,0      ,1

Above is the i/p for the code

My last column shul be sum of all the columns from column 5 to column last.....

Current o/p m getting is a wrong one its upto 8th column...

Code:
Code:
ALE,APESH,id1_0,COMBO   ,4      ,9      ,28     ,8      ,13     ,11     ,14     ,13     ,17,49
ALE,APESH,id2_1_*,COMBO ,0      ,0      ,4      ,3      ,1      ,1      ,2      ,2      ,0,7
ALE,APESH,id2_2,COMBO   ,41     ,25     ,13     ,34     ,40     ,14     ,47     ,18     ,6,113
ALE,APESH,id2_3,COMBO   ,54     ,36     ,53     ,52     ,49     ,28     ,34     ,31     ,1,195
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,2
ALE,APESH,id3_2_*,COMBO ,7      ,6      ,4      ,4      ,2      ,4      ,3      ,3      ,2,21
ALE,APESH,id3_3,COMBO   ,56     ,67     ,43     ,36     ,32     ,57     ,54     ,39     ,0,202
ALE,APESH,id4_2,COMBO   ,1      ,0      ,1      ,2      ,0      ,1      ,0      ,0      ,0,4
ALE,APESH,id4_3,COMBO   ,1      ,0      ,0      ,0      ,1      ,0      ,0      ,0      ,1,1

# 4  
Old 03-04-2013
Code:
awk -F, '{ for (i=5;i<=NF;i++) t+=$i; $NF=$NF "\t" OFS t; t=0 } 1' OFS=, file

# 5  
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

# 6  
Old 03-04-2013
Bipin : ur code did run... but it removed a feature of my prev code.. As don cragun said it dint yield me the total column count in d end..

as in this shown below
Code:
Code:
total,,,,164,144,147,139,138,117,154,106,27,1136

Don cragun: Note that the row totals you showed in message #3 in this thread do not match the data in the columns in those rows. ????
# 7  
Old 03-04-2013
Quote:
Originally Posted by nikhil jain
Bipin : ur code did run... but it removed a feature of my prev code.. As don cragun said it dint yield me the total column count in d end..

as in this shown below
Code:
Code:
total,,,,164,144,147,139,138,117,154,106,27,1136

Don cragun: 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 sample output you showed in message #3 was:
Code:
ALE,APESH,id1_0,COMBO   ,4      ,9      ,28     ,8      ,13     ,11     ,14     ,13     ,17,49
ALE,APESH,id2_1_*,COMBO ,0      ,0      ,4      ,3      ,1      ,1      ,2      ,2      ,0,7
ALE,APESH,id2_2,COMBO   ,41     ,25     ,13     ,34     ,40     ,14     ,47     ,18     ,6,113
ALE,APESH,id2_3,COMBO   ,54     ,36     ,53     ,52     ,49     ,28     ,34     ,31     ,1,195
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,2
ALE,APESH,id3_2_*,COMBO ,7      ,6      ,4      ,4      ,2      ,4      ,3      ,3      ,2,21
ALE,APESH,id3_3,COMBO   ,56     ,67     ,43     ,36     ,32     ,57     ,54     ,39     ,0,202
ALE,APESH,id4_2,COMBO   ,1      ,0      ,1      ,2      ,0      ,1      ,0      ,0      ,0,4
ALE,APESH,id4_3,COMBO   ,1      ,0      ,0      ,0      ,1      ,0      ,0      ,0      ,1,1

Looking at the last row as an example: 1 + 0 + 0 + 0 + 1 + 0 + 0 + 0 + 1 is 3; not 1.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question