LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 07-19-2010, 04:28 AM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 676
Blog Entries: 2

Rep: Reputation: 36
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
 
Old 07-19-2010, 04:44 AM   #2
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
although this is probably possible with shell scripting, it sounds like a typical application for perl:
Code:
#!/usr/bin/perl
use strict;

while (<STDIN>)
{
  if (/^(\d+)\s+(\d+)/)
  {
    print "$1 $2 " . ($1 + $2);
  }
}
 
Old 07-19-2010, 04:47 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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
Note that you'll have to output to a temporary file, and replace the original afterwards.
 
Old 07-19-2010, 05:57 AM   #4
pinga123
Member
 
Registered: Sep 2009
Posts: 676
Blog Entries: 2

Original Poster
Rep: Reputation: 36
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?
 
Old 07-19-2010, 06:07 AM   #5
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
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
 
Old 07-19-2010, 07:03 AM   #6
pinga123
Member
 
Registered: Sep 2009
Posts: 676
Blog Entries: 2

Original Poster
Rep: Reputation: 36
Quote:
Originally Posted by zirias View Post
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
This is helpful but do i really need to learn perl or it can be done using shell script also .

I know perl has lot of such functionality but i wonder how shell script lacks in this feature?
 
Old 07-19-2010, 07:09 AM   #7
Dinithion
Member
 
Registered: Oct 2007
Location: Norway
Distribution: Slackware 14.1
Posts: 443

Rep: Reputation: 56
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
let TMP=$TMP+$i
done
echo $TMP
Now you have to do this for each column, you can use cut to only read a single column

Code:
cut -f 1 -d ' '
will output the first column of a line. Combine this with the first code-block I wrote, and you are near a solution

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.

Last edited by Dinithion; 07-19-2010 at 07:14 AM.
 
Old 07-19-2010, 07:16 AM   #8
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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
sasha@reactor: b=4
sasha@reactor: echo $((a+b))
7
sasha@reactor:
So, not difficult. But you'd need to do some sort of loop to read the file and process it, and dump the results to the new file. If you wanted to process floating-point (decimals/fractions) you would have to incorporate something in addition to bash, such as `bc`, which is the "Basic Calculator", and is quite capable of doing most math you can throw at it.

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
 
Old 07-19-2010, 07:19 AM   #9
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
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)
 
Old 07-19-2010, 07:27 AM   #10
Dinithion
Member
 
Registered: Oct 2007
Location: Norway
Distribution: Slackware 14.1
Posts: 443

Rep: Reputation: 56
Code:
#!/bin/bash

FILE=<input file>
COL=<column>
SUM=0
for i in $(cut -d ' ' -f $COL $FILE); do
        let SUM=$SUM+$i
done
echo $SUM
unset SUM COL FILE
This works for me
This is just problem #4 tough.
 
Old 07-19-2010, 07:30 AM   #11
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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
Row 1: 1 4 Sum=5
Row 2: 2 5 Sum=7
Row 3: 3 6 Sum=9
sasha@reactor:
Those are columns, no?
 
Old 07-19-2010, 07:58 AM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
I also think this is a perfect Perl application.

Code:
my @rows;

while (<STDIN>) {
    if (/([0-9]+)[ \t]+([0-9]+)/) {
        my $sum = $1 + $2;
        push @rows, $sum;
        print "$1 $2 $sum\n";
    }
}
I don't know what you meant by "saving the result to a variable", here I just put all the sums into an array called @rows that you can access later.
 
Old 07-19-2010, 01:44 PM   #13
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
Quote:
Originally Posted by GrapefruiTgirl View Post
Those are columns, no?
Take a close look at posting #4. Although the OP writes "row1" and "row2", he actually gives the columns sums as examples. Your code prints columns, but not their sums (only those of the rows).

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
 
1 members found this post helpful.
Old 07-19-2010, 01:49 PM   #14
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Thanks for pointing that out zirias - now I see
 
Old 07-19-2010, 01:56 PM   #15
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Code:
$ cat pinga.txt 
10 3
20 4 
$ awk '{col1+=$1; col2+=$2; print $1" "$2" "$1+$2}END{print col1 " "col2}' pinga.txt
10 3 13
20 4 24
30 7
$
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
memory calculation mario.almeida Linux - Server 3 07-22-2010 10:05 AM
[SOLVED] complex math calculation in shell script needed gary_in_springhill Programming 33 03-20-2010 05:47 AM
date calculation satish.res Linux - Newbie 3 12-27-2009 05:31 AM
Calculation aid script! assasukasse Programming 24 09-15-2007 12:07 PM
c math calculation alaios Programming 3 06-01-2004 01:46 AM


All times are GMT -5. The time now is 07:32 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration