Arithmetic calculation in variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Arithmetic calculation in variable
# 1  
Old 04-03-2013
Arithmetic calculation in variable

Hi,

I am trying to do an addition to a value stored in a variable...
Code:
var1=`grep -n "match" sample.txt|cut -d : -f1`

var2=`echo $var1|cut -d " " -f1` (Here i want add +1 to the output value)

Last edited by Franklin52; 04-04-2013 at 03:18 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 04-03-2013
Code:
echo $var1|cut -d " " -f1 | xargs echo "1 +" | bc

# 3  
Old 04-03-2013
or
Code:
awk '{print $1+1}' <<< $var1

there are many ways to do it.

--ahamed
# 4  
Old 04-03-2013
Code:
var2=`expr $var2 + 1`

# 5  
Old 04-04-2013
Do you need the contents of var1, or is it just a work variable needed to get the value you want to put into var2? If you just want var2 to be 1 plus the line number of the 1st line in sample.txt that matches the basic regular expression match, calling awk once instead of grep once and cut twice is probably faster:
Code:
var2=$(awk '/match/{print NR + 1; exit}' sample.txt)

but note that when using awk, match is treated as an extended regular expression instead of a basic regular expression. As always, if you're running on a Solaris/SunOS system, use /usr/xpg4/bin/awk or nawk instead of awk.

If you do need the complete list of matching lines, you can keep the way you're currently setting var1, but set var2 just using standard shell parameter expansions:
Code:
var2=$((${var1%% *} + 1))

This should be MUCH MUCH faster than calling cut, xargs, and bc; awk; or cut and expr. This won't work with the original Bourne shell, but works with any POSIX conforming shell (such as bash or a 1993 or later version of ksh).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Arithmetic operation in variable

Hi, Here is the script i try to perform arithmetic operation in two variables . git branch -r | while read brname ; do REV_COMMITS=`git rev-list --count $brname` echo "$brname has $REV_COMMITS" (( TOTAL = TOTAL + REV_COMMITS )) echo "in loop" $TOTAL done echo "total is " $TOTAL ... (3 Replies)
Discussion started by: greet_sed
3 Replies

2. Shell Programming and Scripting

BC calculation for floating (invalid arithmetic operator )

Hi, I wish to compare the CPU LOAD 1 min with 5mins and 15mins. If 1 min's CPU LOAd spike 3% compare to 5 mins or 15 mins CPU Load, it is warning. If 1 min's CPU LOAd spike 5% compare to 5 mins or 15 mins CPU Load, it is critical. However I received following code error, I google it and... (10 Replies)
Discussion started by: alvintiow
10 Replies

3. Programming

arithmetic calculation using awk

hi there again, i need to do a simple division with my data with a number of rows. i think i wanted to have a simple output like this one: col1 col2 col3 val1 val2 val1/val2 valn valm valn/valm any suggestion is very much appreciated. thanks much. (2 Replies)
Discussion started by: ida1215
2 Replies

4. Shell Programming and Scripting

echo multiple variable with calculation

$total=500 echo "scale=2; $val1*100/$total" | bc echo "scale=2; $val2*100*100/$total" | bc echo "scale=2; $val3*100/$total" | bc I want to make the above code to be accomplish in a single echo line. For instance output:21.3, 44.2, 51.6 How to achieve that, some one please help, i just... (5 Replies)
Discussion started by: alvin0618
5 Replies

5. Shell Programming and Scripting

arithmetic from csh variable passed to awk

I have the following code in a csh script I want to pass the value of the variable sigmasq to the awk script so that I can divide $0 by the value of sigmasq grep "Rms Value" $f.log \ | awk '{ sub(/*:*\.*/,x); \ print... (2 Replies)
Discussion started by: kristinu
2 Replies

6. Shell Programming and Scripting

Arithmetic operation between columns using loop variable

Hi I have a file with 3 columns. say, infile: 1 50 68 34 3 23 23 4 56 ------- ------- I want to generate n files from this file using a loop so that 1st column in output file is (column1 of infile/(2*n+2.561)) I am doing like this: for ((i=1; i<=3; i++)) do a=`echo... (3 Replies)
Discussion started by: Surabhi_so_mh
3 Replies

7. Shell Programming and Scripting

Arithmetic calculation on real numbers in Bourne Shell Script

I am begining to learn bourne shell and as a practice I have written a script which when given the purchase price and percentage of discount calculates the savings. I somehow cannot figure out why my script fails to do arthimatic calculation on real numbers. Could anyone look at the script... (5 Replies)
Discussion started by: Tirmazi
5 Replies

8. UNIX for Dummies Questions & Answers

Arithmetic: how to??

Hello all, I'd like to know how to perform arithmetic on multiple files. I have got many tab-delimited files. Each file contains about 2000 rows and 2000 columns. What I want to do is to to sum the values in each row & column in every file. The following explains what I want to do; ... (9 Replies)
Discussion started by: Muhammad Rahiz
9 Replies

9. Shell Programming and Scripting

arithmetic in tcsh

Yes I know tcsh sucks for scripting and arithmetic but I have to write a script for multiple users and they all use tcsh. I have this variable that I 'set' with but pulling numbers off of stings with set STUFF = `grep string file | awk command` Now I would like to add up the numbers that... (4 Replies)
Discussion started by: gobi
4 Replies

10. UNIX for Dummies Questions & Answers

arithmetic problem

i am used to making scripts for hp-ux. but lately i tried to make some for solaris. the problem is that when i tried to execute it it gave me an error the "let: not found". why is that? how can i perform an arithmetic function in the solaris shell script? thanks :) (2 Replies)
Discussion started by: inquirer
2 Replies
Login or Register to Ask a Question