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 302515633 by thulasidharan2k on Wednesday 20th of April 2011 02:40:48 PM
Old 04-20-2011
Power

Hi Corona688, instead of using the following code to assign variables cs3, cs4, cs5, cs6, cs7, cs8 and cs9, I used the for loop mentioned in the thread. But, the for loop is not working. Please help.
Code:
awk -F"|" 'BEGIN{sum=0}{sum=sum+$3}END{printf("%d\n", sum)}' css
awk -F"|" 'BEGIN{sum=0}{sum=sum+$4}END{printf("%d\n", sum)}' css
awk -F"|" 'BEGIN{sum=0}{sum=sum+$5}END{printf("%d\n", sum)}' css
awk -F"|" 'BEGIN{sum=0}{sum=sum+$6}END{printf("%d\n", sum)}' css
awk -F"|" 'BEGIN{sum=0}{sum=sum+$7}END{printf("%d\n", sum)}' css
awk -F"|" 'BEGIN{sum=0}{sum=sum+$8}END{printf("%d\n", sum)}' css
awk -F"|" 'BEGIN{sum=0}{sum=sum+$9}END{printf("%d\n", sum)}' css

The file css looks like below. I want to add the values in each column and store them in variables for each column
00:00 to 01:00|666|666|666|0|0|0|0|0|0|0|0
01:00 to 02:00|37|2|37|0|0|0|0|0|0|0|0
02:00 to 03:00|1860|1860|1860|0|0|0|0|0|0|0|0
03:00 to 04:00|892|890|890|0|0|0|0|0|0|0|0
04:00 to 05:00|1586|1588|1588|0|0|0|0|0|0|0|0
05:00 to 06:00|1912|1912|1912|0|0|0|0|0|0|0|0
06:00 to 07:00|2815|2815|2815|0|0|0|0|0|0|0|0
07:00 to 08:00|2497|2497|2497|0|0|0|0|0|0|0|0
08:00 to 09:00|3611|3611|3611|0|0|0|0|0|0|0|0
09:00 to 10:00|4903|4881|4881|0|0|0|0|0|0|0|0
10:00 to 11:00|5624|5642|5642|0|0|0|0|0|0|0|0
11:00 to 12:00|5617|5611|5611|0|0|0|0|0|0|0|0
12:00 to 13:00|7877|6505|6484|21|0|0|0|0|0|0|0
13:00 to 14:00|6608|7110|3202|2213|1511|184|0|0|0|0|0
14:00 to 15:00|5596|6469|4705|1764|0|0|0|0|0|0|0
15:00 to 16:00|5005|5003|5003|0|0|0|0|0|0|0|0
16:00 to 17:00|4220|4229|4229|0|0|0|0|0|0|0|0
17:00 to 18:00|7769|7771|7771|0|0|0|0|0|0|0|0
18:00 to 19:00|6626|6626|6626|0|0|0|0|0|0|0|0
19:00 to 20:00|4852|4852|4852|0|0|0|0|0|0|0|0
20:00 to 21:00|5758|5758|5758|0|0|0|0|0|0|0|0
21:00 to 22:00|5019|5019|5019|0|0|0|0|0|0|0|0
22:00 to 23:00|1840|1840|1840|0|0|0|0|0|0|0|0
23:00 to 24:00|159|159|159|0|0|0|0|0|0|0|0
 

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
foreach(n)						       Tcl Built-In Commands							foreach(n)

__________________________________________________________________________________________________________________________________________________

NAME
foreach - Iterate over all elements in one or more lists SYNOPSIS
foreach varname list body foreach varlist1 list1 ?varlist2 list2 ...? body _________________________________________________________________ DESCRIPTION
The foreach command implements a loop where the loop variable(s) take on values from one or more lists. In the simplest case there is one loop variable, varname, and one list, list, that is a list of values to assign to varname. The body argument is a Tcl script. For each element of list (in order from first to last), foreach assigns the contents of the element to varname as if the lindex command had been used to extract the element, then calls the Tcl interpreter to execute body. In the general case there can be more than one value list (e.g., list1 and list2), and each value list can be associated with a list of loop variables (e.g., varlist1 and varlist2). During each iteration of the loop the variables of each varlist are assigned consecutive values from the corresponding list. Values in each list are used in order from first to last, and each value is used exactly once. The total number of loop iterations is large enough to use up all the values from all the value lists. If a value list does not contain enough elements for each of its loop variables in each iteration, empty values are used for the missing elements. The break and continue statements may be invoked inside body, with the same effect as in the for command. Foreach returns an empty string. EXAMPLES
This loop prints every value in a list together with the square and cube of the value: set values {1 3 5 7 2 4 6 8} ;# Odd numbers first, for fun! puts "Value Square Cube" ;# Neat-looking header foreach x $values { ;# Now loop and print... puts " $x [expr {$x**2}] [expr {$x**3}]" } The following loop uses i and j as loop variables to iterate over pairs of elements of a single list. set x {} foreach {i j} {a b c d e f} { lappend x $j $i } # The value of x is "b a d c f e" # There are 3 iterations of the loop. The next loop uses i and j to iterate over two lists in parallel. set x {} foreach i {a b c} j {d e f g} { lappend x $i $j } # The value of x is "a d b e c f {} g" # There are 4 iterations of the loop. The two forms are combined in the following example. set x {} foreach i {a b c} {j k} {d e f g} { lappend x $i $j $k } # The value of x is "a d e b f g c {} {}" # There are 3 iterations of the loop. SEE ALSO
for(n), while(n), break(n), continue(n) KEYWORDS
foreach, iteration, list, looping Tcl foreach(n)
All times are GMT -4. The time now is 03:18 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy