Subtracting values from variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Subtracting values from variable
# 1  
Old 10-12-2012
Subtracting values from variable

Legends,

Please help me in , how do i subtract the variable values listed like below.
the first value of orig should be subtracted from first value of prev and so on.

Code:
san> echo $orig
346 316 340 239 410 107 291 139 128 230 167 147 159 159 172 116 110 260 177 0 177 169 168 186 165 366 195 131 7 143 60 276 149 114 120 110 103 5 0 87 79 104 101 142 184 13 278 2 153 160 0 5 101 127 151 14 196 138 90 177 195 166 135 122 136 117 169 518 244 125 116 127 117 131 129 121 120 164 218 105 111 122 127 123 117 168 536 192 177 133 130 160 78 126 678 277 294 297 842 78

san> echo $prev
346 316 340 239 410 107 291 139 128 230 166 147 159 159 172 116 110 260 177 0 177 167 168 186 165 366 195 131 7 143 60 276 145 114 120 110 103 5 0 87 79 101 99 140 184 13 278 2 153 160 0 5 99 125 150 14 196 136 87 177 195 162 135 121 136 113 169 518 244 125 116 127 117 131 129 119 117 164 218 101 111 122 127 123 116 168 536 192 177 133 130 160 78 126 678 277 294 297 842 78

Pls help
# 2  
Old 10-12-2012
try something like this..

assuming both variables have same number of fields..

Code:
echo "" | awk -v FV="$orig" -v SV="$prev" '{ n=split(FV,F);split(SV,S)}END{
for(i=1;i<=n;i++){printf (S[i] - F[i])" "}}'

I am not able to use just awk..Smilie

any guess?

Last edited by pamu; 10-12-2012 at 10:04 AM..
# 3  
Old 10-12-2012
i Tried the follwoing using array but no go

Code:
for (( i = 0 ; i < ${#orig[@]} ; i++ ))
do
for (( j = 0 ; j < ${#prev[@]} ; j++ ))
do
if [ $i == $j ];
then
(( DIFF = ${orig[i]} - ${prev[j]} ))
echo $DIFF;
else
echo ${orig[i]}  ${prev[j]} > /dev/null 2>&1
fi
done
done

---------- Post updated at 04:51 AM ---------- Previous update was at 04:48 AM ----------

@pamu

below is the error i am gettng

Code:
> echo "" | awk -v FV="$orig" -v SV="$prev" '{ n=split(FV,F);split(SV,S)}END{for(i=1;i<=n;i++){printf (F[i] - S[i])" "}}'
awk: syntax error near line 2
awk: bailing out near line 2

# 4  
Old 10-12-2012
try using nawk
# 5  
Old 10-12-2012
In ksh93/bash (assuming that there are equal number of elements in both variables):
Code:
orig_array=($orig)
prev_array=($prev)
diff=
for((i=0;i<${#orig_array[*]};i++))
do
 diff="$diff $(( ${prev_array[i]} - ${orig_array[i]} ))"
done

echo $diff

producing
Code:
0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 -2 0 0 0 0 0 0 0 0 0 0 -4 0 0 0 0 0 0 0 0 -3 -2 -2 0 0 0 0 0 0 0 0 -2 -2 -1 0 0 -2 -3 0 0 -4 0 -1 0 -4 0 0 0 0 0 0 0 0 0 -2 -3 0 0 -4 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

This User Gave Thanks to elixir_sinari For This Post:
# 6  
Old 10-12-2012
@elixir,
main requirement is: i have two files File A and File B
using these two files, i want out with difference to third one (say file C) after calculating diff from 3rd column from file
using "bash" shell

Code:
file A
abc             xxx  340 
def             yyy  239 
ghi             zzz  410 

file B
abc             xxx  330 
def             yyy  230 
ghi             zzz  310

OUTPUT SHOLUD be
Code:
File C
abc             xxx  10 times diff
def             yyy  9 times diff
so on...

# 7  
Old 10-12-2012
Quote:
Originally Posted by sdosanjh
how do i subtract the variable values listed like below.
Quote:
Originally Posted by sdosanjh
@elixir,
main requirement is: i have two files File A and File B
There is a big difference between how you handle files and variables. You mention variables in one post (and in the thread title) and files in the other.

Please don't try to oversimplify the problem. This makes it difficult to provide a proper solution.
This User Gave Thanks to elixir_sinari For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Changing CSV files with date . Subtracting date by values

Hi All, I have a CSV file which is as below. Basically I need to take the year column in it and find if the year is >= 20152 . If that is then I should subtract all values by 6. In the below example in description I am having number mentioned as YYWW so I need to subtract those by -5. Whereever... (8 Replies)
Discussion started by: arunkumar_mca
8 Replies

2. Shell Programming and Scripting

Variable with multiple values

Hello I need to alter a script to check for a flag file but there are now more than one type of flag file in my directory. Basically if any of these flg files exist then the MASK value should be set? Flag files to be included in assignment to variable e2e_scheduled*.flg COLL_STOP*.flg... (1 Reply)
Discussion started by: andymay
1 Replies

3. Shell Programming and Scripting

Filtering values in variable

Hi, is there a faster/simpler way to filter values from the variable1 in variable2? example: variable1="A|B|C|E" variable2="A|B|C|D|F" output: "A|B|C" Thanks, zzavilz (4 Replies)
Discussion started by: zzavilz
4 Replies

4. Shell Programming and Scripting

Searching columns and subtracting values in awk

Hi everyone, I had a similar question a couple days ago but my problem has gotten significantly (to me anyway) more complex. I have two files: File 1: 0808 166 166 62 9 0 1000fights 1 1 2 1 0 100places2visit 2 2 2 2 0 10veronica91 167 167 3 1 0 11thgorgeous 346 346 3806 1461 122... (2 Replies)
Discussion started by: collards
2 Replies

5. UNIX for Dummies Questions & Answers

Subtracting values from 2 columns in a file

Hello, I have a file with 5 columns that looks like this: A1BG chr19 + 58863335 58866549 A1BG chr19 - 58858171 58864865 A2LD1 chr13 - 101182417 101186056 A2LD1 chr13 - 101182417 101241046 A2M chr12 - 9220303 9268558 A2ML1 ... (5 Replies)
Discussion started by: wolf_blue
5 Replies

6. Shell Programming and Scripting

Assign values to variable

Hi Masters, I want to assign the values of one variable to another variable. Here the varaible name 'var' is dynamic. I know the values of V_2 and U_3, but If the i/p of TYPE is 'U' and the NO is 3, then I want to assign the values of U_3 to var. How we can achieve it? TYPE="U"... (4 Replies)
Discussion started by: ecearund
4 Replies

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

8. UNIX for Dummies Questions & Answers

Dynamic variable values

Bit of a newbie :D with regard to unix scripting and need some advice. Hopefully someone can help with the following: I have a predefined set of variables as follows: AAA_IP_ADD=1.1.1.1 BBB_IP_ADD=2.2.2.2 I have a funnction call which retrieves a value into $SUPPLIER which would be... (3 Replies)
Discussion started by: ronnie_uk
3 Replies

9. UNIX for Dummies Questions & Answers

Subtracting an Integer from a Variable

Hello, I am in following situation.- COUNT=`ls -l | wc -l` echo $COUNT ---> 26 NO_OF_FILES=$COUNT-1 echo $NO_OF_FILES ---> 26-1 Here, I want the output to be 25. How could I do this. It seems simple, but I am not getting it. Please help me. (2 Replies)
Discussion started by: The Observer
2 Replies

10. Shell Programming and Scripting

Storing values in variable

Hi All, Here is the description of the problem. I am scripting for database access using k-shell on solaris box dbaccess <databasename> - << EOF 2>/dev/null | awk 'BEGIN {FS=" "}\ {printf "%s", $1}' | grep -v "^$" | \ read cnt1 OUTPUT TO PIPE cat WITHOUT HEADINGS select count(*) from... (1 Reply)
Discussion started by: matrixmadhan
1 Replies
Login or Register to Ask a Question