Subtraction while reading a variable value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Subtraction while reading a variable value
# 1  
Old 11-30-2011
Subtraction while reading a variable value

Hi Guru's

i have a file which contains data as below(file.txt) and an inital value 8725

403
352
462
359
317
347
414
285
308
350
300
305
381
319
373
358
329
318
360
313
338
779
368

how would i read each value in a file and subtract it from the inital value?

i tried using below format, But it failed with syntax error.

Code:
state3=8725
while read $ct 
do
TOTAL=`expr $state3 - $ct`
echo $TOTAL
done < file.txt

Please advice how to achieve this?
# 2  
Old 11-30-2011
can u tell us what error u r getting??
# 3  
Old 11-30-2011
here's the error

expr: syntax error
expr: syntax error
expr: syntax error
expr: syntax error
expr: syntax error
expr: syntax error
expr: syntax error
expr: syntax error
expr: syntax error
expr: syntax error
expr: syntax error
expr: syntax error
# 4  
Old 11-30-2011
Also use code tags when posting error logs, example data, code snippets, thanks. Indention of code can also be useful.

Remove the $:
Code:
while read ct

This User Gave Thanks to zaxxon For This Post:
# 5  
Old 11-30-2011
change the below line and execute...

Code:
while read ct

its not $ct
This User Gave Thanks to siva shankar For This Post:
# 6  
Old 11-30-2011
Opps!! Thanks guys Smilie Smilie works now

---------- Post updated at 09:01 PM ---------- Previous update was at 08:06 PM ----------

One more update to it.

the output should be in format
Code:
$state3 - $(addition of all number's in file.txt)

the current code captures one value of file.txt and subtracts.
# 7  
Old 11-30-2011
Code:
# perl -le '$x=0;$state3=8725;open I,"<input";while(<I>){$x+=$_} print $state3-$x;close I'
287

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem reading into Variable using KSH

hi all, i create 1 file.txt and inside it contain : file1 file2 file3 file 4 then output that i want in my script: $1=file1 $2=file2 $3=file3 $4=file4 but,when write in ksh script with: while read folder set - $( echo ${folder} ) i=1 while (($i <= $#)) do ... (2 Replies)
Discussion started by: proghack
2 Replies

2. Shell Programming and Scripting

Reading a variable in csh

I have a simple script that sets a value and reads the value in csh: set -x set a = 10 echo $a The output of the script does not show the value of a + set a = 10 + echo any help would be great. (4 Replies)
Discussion started by: pt14
4 Replies

3. Shell Programming and Scripting

reading variable value from a file

Hello, I need to read a variable value from script. Below is the scenario I am reading a value from an external file say a.txt a.txt: Jan Feb Mar I need the corresponding value of the months in in numerics such as Jan -->1, Feb-->2 etc. I have this mapping in another file... (1 Reply)
Discussion started by: aixjadoo
1 Replies

4. UNIX for Dummies Questions & Answers

Reading a variable from file

Hi, I have a situation where I need to read a variable from another file. But the problem is that the variable in the other file is starting with $. E.g. file1: $var1=out temp_ss.sh: . file1 echo "Print : $var1" It works fine if the file1 is having var1=out (note that it is... (6 Replies)
Discussion started by: shash
6 Replies

5. UNIX for Dummies Questions & Answers

assingn a variable a filename and then reading it in

Im trying to set a filename to a variable and then read the file in using the variable but im getting a syntax error. any ideas? #!/bin/bash function scanFile() { while read $1 do echo $filename done } file1=report.log scanFile() $file1 (3 Replies)
Discussion started by: magnia
3 Replies

6. Shell Programming and Scripting

Reading variable from file variable values

Hi, Here is the output of lpstat. I would like to read value of Queue which is(abxxxxb1)and status that is DOWN in first line. i dont care what is in second line. any one can help me.thanks Queue Dev Status Job Files User PP % Blks Cp Rnk ------- ----- ---------... (5 Replies)
Discussion started by: sagii
5 Replies

7. UNIX for Dummies Questions & Answers

reading more than one variable into a for loop

Hi, I have a file (details.txt) with 3 rows of variables ie... name postcode age john D fr25dd 25 mark W ab122aa 22 phil C cd343bb 33 What I want to do is read down the list with a loop and add each field into a one line piece of text... So I have a file (test1) which reads;... (3 Replies)
Discussion started by: starsky
3 Replies

8. Shell Programming and Scripting

reading ~/.bashrc variable

1) I've added a variable called IMPORT_HOME to my ~/.bashrc file: IMPORT_HOME=/import:$IMPORT_HOME 2) I sourced the bashrc file: source ~/.bashrc 3) In my bash script, i tried to echo out the IMPORT_HOME variable but it doesnt print out '/import/, only whitespace: #!/bin/bash echo... (2 Replies)
Discussion started by: nuGz
2 Replies

9. UNIX for Advanced & Expert Users

Urgent-reading a variable value

Hi, I have a text file in which , I have contents like t1=abc t2=xyz t3=awe ...... I am able to read contents of these variables in a script by . /temp.txt echo $t1 Now, what my requirement is something like this a="t" i=1 echo $a$i --->this is displaying t1 (3 Replies)
Discussion started by: kaaakrishna
3 Replies

10. Shell Programming and Scripting

perl not reading my variable

I'm trying to make changes in a file using the following bash script: #!/bin/bash MYHOME=`echo $HOME` README=$MYHOME"/environment" IAM=`whoami` CHANGEPATHLIST="TALOG TACONFIG TAINFO TAWORK TMPSPACE" for var in $CHANGEPATHLIST do perl -pi -e 's/sacuser1/$IAM/ if m/$var/' $README... (3 Replies)
Discussion started by: yoonixq4u
3 Replies
Login or Register to Ask a Question