Sponsored Content
Top Forums Shell Programming and Scripting Need help in assigning output of n commands to n variables automatically inside a for loop Post 302515634 by Corona688 on Wednesday 20th of April 2011 02:48:38 PM
Old 04-20-2011
If it's just numbers like 1|2|3|4, then

Code:
#!/bin/ksh

IFS="|"
# Read individual lines from inputfile
while read LINE
do
        # Split $LINE apart on IFS into the array CSI
        set -A CSI $LINE

        # Start at column 1.  Column 0 would be that time column
        N=1
        while [ ! -z "${CSI[$N]}" ]
        do
                # If the column is blank, set it to zero
                [ -z "${CS[$N]}" ] && ((CS[$N]=0))
                # Add the number to the column
                ((CS[$N] += CSI[$N]))
                # Add one to N
                ((N++))
        done
done < inputfile

N=1
while [ ! -z "${CS[$N]}" ]
do
        echo "cs$N=${CS[$N]}"
        ((N++))
done

Code:
$ cat >inputfile <<EOF
1|2|3|4
5|6|7|8
9|10|11|12
EOF
$ ./addcol.sh
cs1=18
cs2=21
cs3=24
$

---------- Post updated at 12:45 PM ---------- Previous update was at 12:41 PM ----------

Updated to fit your data better:

Code:
#!/bin/ksh

IFS="|"
# Read individual lines from inputfile
while read TIME LINE
do
        # Split $LINE apart on IFS into the array CSI
        set -A CSI $LINE

        N=0
        while [ ! -z "${CSI[$N]}" ]
        do
                [ -z "${CS[$N]}" ] && ((CS[$N]=0))
                ((CS[$N] += CSI[$N]))
                ((N++))
        done
done < inputfile

N=0
while [ ! -z "${CS[$N]}" ]
do
        echo "cs$N=${CS[$N]}"
        ((N++))
done

---------- Post updated at 12:48 PM ---------- Previous update was at 12:45 PM ----------

...or just do it all in awk then read it once into the shell:

Code:
LINE="`awk -v FS='|' '{ for(N=2; N<=NF; N++) { T[N]+=$N; MAX=N; } }
END { for(N=2; N<=MAX; N++) printf("%s ", T[N]); printf("\n"); }' inputfile`"

set -A CS $LINE

N=0
while [ ! -z "${CS[$N]}" ]
do
        echo "CS$N=${CS[$N]}"
        ((N++))
done

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

assigning variables

Before I even attempt this, is it possible to grep for a pattern, maybe a partial sentence like "go to page 3", assign that to a variable and then use awk or something to pull out the 3 and assign it to a variable? So first I would have Gotopg = "go to page 3" then page = 3 (9 Replies)
Discussion started by: k@ssidy
9 Replies

2. Shell Programming and Scripting

Assigning nawk output to variables

I do a lot of command line scripting to capture data from files or other command output. I've checked in a number of Unix and scripting books but for the life of me I can't find out how to asign field data from nawk output into variables that I can manipulate later. For example, reading a two... (6 Replies)
Discussion started by: steveje0711
6 Replies

3. UNIX for Dummies Questions & Answers

assigning variables from standard output

What am I doing wrong? I was searching for the answer to assigning variables from output. I found this simple response ls -l apply_want.m | read perms links owner group size mtime1 mtime2 mtime3 file this should allow me to echo the variables echo "$perms | $links | $owner | $group |... (2 Replies)
Discussion started by: whamchaxed
2 Replies

4. Shell Programming and Scripting

Assigning inside for loop

If I have 3 variables and I want to check if any of these is null. If one of them is null then it should be assigned a value of 0.I have the following code below. The output should be 0 is A, 11 is B, 33 is C $a= $b=11 $c=33 $echo $a $b $c $11 33 for i in "${a}" "${b}" "${c}"; do ... (2 Replies)
Discussion started by: thana
2 Replies

5. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

6. Solaris

Assigning Temp IP inside zones

I want to assign ip to a zone , but i dont want that ip to be retained when the zone is rebooted , is there a way to do it ? Thx (7 Replies)
Discussion started by: skamal4u
7 Replies

7. Shell Programming and Scripting

How to give a variable output name in a shell script inside a for loop

Hi all I run my program prog.c in the following way : $ ./prog 1 > output.txt where 1 is a user defined initial value used by the program. But now I want to run it for many a thousand initial values, 1-1000, and store all the outputs in different files. Like $ ./prog 1... (1 Reply)
Discussion started by: alice06
1 Replies

8. Shell Programming and Scripting

problem in assigning substr to a variable inside awk

Hi All, I have a fixed-width datafile from which i need to extract value/string starting from some position to the specified length in each of the lines. awk '{print substr($0,x,y)}' datafile --- is working fine but awk 'BEGIN{a=0}{a=substr($0,x,y);print $a}' datafile ---is giving... (3 Replies)
Discussion started by: loggedin.ksh
3 Replies

9. Shell Programming and Scripting

Getting phone number, its message and assigning them into 2 variables then screen output.

Hi Everyone, I have a flatfile "inbox.txt" which contains some information: Location 0, folder "Inbox", SIM memory, Inbox folder SMS message SMSC number : "+24800000023" Sent : Sat 04 Aug 2012 09:01:00 PM +0700 Coding : Default GSM alphabet... (5 Replies)
Discussion started by: testcase
5 Replies

10. Shell Programming and Scripting

Assigning values to 2 variables within for loop

Hi All, Is it possible to grep for two files and assign their names to two separate variables with for loop? I am doing the below currently: if then for fname in $( cd $dirA ; ls -tr | grep "^Ucountry_file$") do InFile=$dirA/$fname ... (4 Replies)
Discussion started by: swasid
4 Replies
break(1)							   User Commands							  break(1)

NAME
break, continue - shell built-in functions to escape from or advance within a controlling while, for, foreach, or until loop SYNOPSIS
sh break [n] continue [n] csh break continue ksh *break [n] *continue [n] DESCRIPTION
sh The break utility exits from the enclosing for or while loop, if any. If n is specified, break n levels. The continue utility resumes the next iteration of the enclosing for or while loop. If n is specified, resume at the n-th enclosing loop. csh The break utility resumes execution after the end of the nearest enclosing foreach or while loop. The remaining commands on the current line are executed. This allows multilevel breaks to be written as a list of break commands, all on one line. The continue utility continues execution of the next iteration of the nearest enclosing while or foreach loop. ksh The break utility exits from the enclosed for, while, until, or select loop, if any. If n is specified, then break n levels. If n is greater than the number of enclosing loops, the outermost enclosing loop shall be exited. The continue utility resumes the next iteration of the enclosed for, while, until, or select loop. If n is specified then resume at the n- th enclosed loop. If n is greater than the number of enclosing loops, the outermost enclosing loop shall be used. On this man page, ksh(1) commands that are preceded by one or two * (asterisks) are treated specially in the following ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. Words that follow a command preceded by ** that are in the format of a variable assignment are expanded with the same rules as a vari- able assignment. This means that tilde substitution is performed after the = sign, and also that word splitting and file name genera- tion are not performed. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
csh(1), exit(1), ksh(1), sh( 1), attributes(5) SunOS 5.10 17 Jul 2002 break(1)
All times are GMT -4. The time now is 11:30 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy