little calculation in script.
How would i calculate following values.
Initial file 10 3 20 4 How would i calculate 3rd column which should be addition of value in 1 and 2nd column. File after calculation 10 3 13 20 4 24 |
although this is probably possible with shell scripting, it sounds like a typical application for perl:
Code:
#!/usr/bin/perl |
Could you give more details please? Are the numbers only whole integers or do they involve decimals, negative numbers, or such? Is there anything in the file except the numbers? What is the exact situation?
Assuming a simple file though, awk should be enough. Code:
awk '{print $1,$2,$1+$2}' file.txt >newfile.txt |
Thanks for your quick replies.
10 3 20 4 How would i calculate the total in each row for example row1total=30 row2total=7 10 3 20 4 also wanted to know how to save the output of above calculation in variable? |
You mean .. the sum of each column?
Ok, now I'd really suggest to learn perl, it was initially done for things like that :) basic idea: declare an array for the columns (containing one array reference per column). loop over the lines like shown in the little perl snippet above, but storing each time the values found in the correct column-array. use these later to calculate your sums. It will take you a while :) |
Quote:
I know perl has lot of such functionality but i wonder how shell script lacks in this feature? |
This is possible, but awk and perl is more slick then bash scripts.
to summarize a column, you can do this f.eks. like this: Code:
for i in $(cat inputfile); do Code:
cut -f 1 -d ' ' Edit: Ops, seems like I was a little trigger happy there. It looks like for read a file word by word, and not line by line. Then another solution is needed. |
Bash/shell is not 'lacking' this feature; you can do basic arithmetic using shell scripting (i.e. Bash shell, or your choice of shell I suppose) but Bash does not do floating-point, only whole-integer operations. For example:
Code:
sasha@reactor: a=3 I believe the AWK solution offered by David in post #3 is going to be the most practical, simple, easy-to-understand way to do what you want. You could eventually come up with a dozen+ variations on how to do what you ask, but post #3 is the neatest. If you absolutely MUST use 100% Bash, do a little reading/research about the bash functions you think you will need (read function, for/while loops, echo command, shell redirection, etc..), begin to construct a script and show us what you've got, and we will help you with your script. Sasha |
Although I like post #3 for its simplicity, it only solves the problem from #1 (calculate the sums of the rows), not that from #4 (columns) ;)
|
Code:
#!/bin/bash This is just problem #4 tough. |
The output is columnized, as far as I can see. To demonstrate:
Code:
sasha@reactor: awk '{print "Row "NR": "$1" "$2" Sum="$1+$2}' mathsed |
I also think this is a perfect Perl application.
Code:
my @rows; |
Quote:
If the intention of the OP really is to sum up rows as well as columns, he's probably even going further -- this is perfectly suited for perl :) |
Thanks for pointing that out zirias - now I see ;)
|
Code:
$ cat pinga.txt |
Ok, i admit, i never learned awk ... seems quite powerful, too
|
Quote:
|
I have started developing the script as said.
This is how my script looks. it takes the output of free -m and insert into a file for given amount of time. Code:
#!/bin/bash Code:
# ./test This can be done by taking col3/col2 but i m getting following error. Code:
# cat /tmp/raminfo | awk '{print $3/$2}' |
1) The error is probably coming from this line:
Code:
RAM Information for last 10 Seconds Code:
awk '/^Mem:/ { print $3 / $2}' /tmp/raminfo 3) Finally a suggestion/idea which is maybe just a matter of preference for me: when doing simple shell math, you could use i=$((i+1)) instead of i=$( expr $i + 1 ) if for no other reason than that there are three less characters to type :D but it's up to you. Cheers! |
Quote:
|
All times are GMT -5. The time now is 01:36 AM. |