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:
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:
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).
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)
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)
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)
$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)
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)
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)
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)
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)
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)
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)