trying to accumulate a number in the same variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting trying to accumulate a number in the same variable
# 1  
Old 12-23-2010
trying to accumulate a number in the same variable

Hello,

I trying to accumulate the same number in a variable:

Code:
#!/usr/bin/sh

infile="$1/$2"

if [[ ! -r $infile ]]
then
        #echo "file is not readable: $infile"
        exit 1
fi

awk '
{       

num_cols=NF;  # the number of columns in the current line
temp_cols=temp_cols+num_cols  #add up no. of columns over multiple lines

}' <$infile >tst.txt

It is a shell script called t.sh. I execute it from the command line like:

print temp_cols;

Code:
./t.sh . inputfile.txt

The idea is that it's supposed to count the columns on each line and add them up except it doesn't.

The variable temp_cols always seems to contain the value of num_cols.

So if the input is this:

Code:
1 1 1
2 2 2
1 1 1

then it returns as output:

Code:
3
3
3

it should return as ouput

Code:
3
6
9

# 2  
Old 12-23-2010
Code:
awk '
{       
num_cols=NF;  # the number of columns in the current line
temp_cols=temp_cols+num_cols  #add up no. of columns over multiple lines
print temp_cols
}' <$infile >tst.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace variable number of character

hi All, My requirement is to replace variable number of in between character with X The first 6 and last 4 characters will remain same and rest characters will be masked with X Example - input String 12345678912345 output 123456XXXX2345 input string 123456 ... (1 Reply)
Discussion started by: Pratik4891
1 Replies

2. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies

3. Shell Programming and Scripting

variable number of input files

Hi all, I'm an extremely newbie with unix/perl. I have a perl script that I need to run on a variable number of input files (i.e., I have to run the perl script on each file, but from run to run, I have a different list of files that need to be put through this script). I think this can... (2 Replies)
Discussion started by: sunmatrix
2 Replies

4. Shell Programming and Scripting

Check if a variable is a number - perl

Logic of code if ( $var is a number ) { Do something } else { Do something else } My question is: How do I check if a variable is a number. All the reg ex that I came up with to match this is failing. Please help. (3 Replies)
Discussion started by: garric
3 Replies

5. Shell Programming and Scripting

Accumulate counter in script

Hi, I'm new to unix and have a problem? I'm writing a basic script in ksh and it is a basic quiz with 5 questions. I need to be able to accumulate the correct answers at the end and echo out the total correct answers, I cannot work it out? Please see script so far. If anyone can help that will... (2 Replies)
Discussion started by: Pablo_beezo
2 Replies

6. UNIX for Dummies Questions & Answers

appending variable number of files

In a particular path of a server I have number of files.The files are generated every date with a date_mth stap on this.There are different files for different clients. For example in /data1 path i have X_0416_Score Y_0416_Score Z_0417_Score X_0417_Score A_0417_Score If i will run the... (1 Reply)
Discussion started by: dr46014
1 Replies

7. Shell Programming and Scripting

Export variable as number

Hi Guys, I am using the korn shell. I have an environments files where I have defined a variable export START_TIME=060000 export END_TIME=220000 I source this environments file into one of my scripts. The problem is that when I try to use this variable, I cannot get to use this variable as a... (2 Replies)
Discussion started by: zeus101
2 Replies

8. Shell Programming and Scripting

How do one evaulate a variable's value if it is number or not

How do one evaulate a variable's value if it is number or not. say var1=1234; .... I want to check if $var1 contains a number or not. Is there any built-in command or combination of commands available? Thanks, Suman (1 Reply)
Discussion started by: suman_jakkula
1 Replies

9. UNIX for Dummies Questions & Answers

defining a variable as a number or character?

I am writing a script that needs to accept numbers into a variable by prompting and using the code read num case $num in 000) break ;; *) I am fairly new to unix and am loving the versatility of the language but need a little shove in the right... (1 Reply)
Discussion started by: noobian
1 Replies

10. UNIX for Dummies Questions & Answers

Check if variable is a number

If I have a variable $X, how do I check it is a number? Many thanks. (2 Replies)
Discussion started by: handak9
2 Replies
Login or Register to Ask a Question