LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-22-2012, 04:24 PM   #1
codergeek
Member
 
Registered: Dec 2012
Posts: 52

Rep: Reputation: 7
Help with subtracting column 2 and column 1 and print the result


hello,

I have a file with two columns and I want to calculate each line subtracting column 2 from column 1 and printing the results.

This is a sample

Column 1 Column 2
101.885086 198.848648
413.796692 517.166626
982.848511 1042.157715
1109.808716 1301.984009
1663.678711 1737.635864

So I want to subtract 198.848648 from 101.885086 which equals 96.963562

Here is what I done thus far:

Code:
while read line
do
a=$(awk '{ print $2 }'  numbers | head -n 1)
b=$(awk '{ print $1 }'  numbers | head -n 1)
sub=$(echo $a -$b | bc )

echo "a = $a b = $b minus=$sub" 
done < numbers
And here is the output

a = 198.848648 b = 101.885086 minus=96.963562
a = 198.848648 b = 101.885086 minus=96.963562
a = 198.848648 b = 101.885086 minus=96.963562
a = 198.848648 b = 101.885086 minus=96.963562
a = 198.848648 b = 101.885086 minus=96.963562

It did the first line but skipped the others.

Any ideas???

Last edited by codergeek; 12-22-2012 at 04:30 PM.
 
Old 12-22-2012, 04:52 PM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Code:
while read line
do
    a=$(awk '{ print $2 }'  numbers | head -n 1)
    ...
done < numbers
1) You read the lines from the numbers file to the line variable,but then you just ignore it and read the file again by awk. head will allways output the first line of what it gets. So, if you just let awk read from line, it should work. There's also no need to use head command, since the while loop gives you one line at a time:

Code:
while read line
do
a=$(awk '{ print $2 }' <<< "$line")
b=$(awk '{ print $1 }'  <<< "$line")
sub=$(echo $a -$b | bc )

echo "a = $a b = $b minus=$sub" 
done < numbers
2) There are some nice things in bash that might help you. Instead of reading the entire line into one variable and then awking it apart, you can do

Code:
while read a b
    ...
done < numbers
that will keep putting the first column into a, and the second one into b

3) Perhaps the nicest way would be to just use one awk:

Code:
awk '{printf("a = %f b = %f minus = %f\n", $1, $2, $1 - $2); }' numbers

Last edited by millgates; 12-22-2012 at 04:55 PM.
 
2 members found this post helpful.
Old 12-22-2012, 05:10 PM   #3
codergeek
Member
 
Registered: Dec 2012
Posts: 52

Original Poster
Rep: Reputation: 7
Greatly appreciate it. It worked like a charm

+1 rep to you
 
Old 12-23-2012, 11:44 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by millgates View Post
Perhaps the nicest way would be to just use one awk:
Code:
awk '{printf("a = %f b = %f minus = %f\n", $1, $2, $1 - $2); }' numbers
I like this but don't understand what the %f bought. I coded it in fewer keystrokes this way ...
Code:
awk '{print("a="$1 " b="$2 " minus="$1-$2)}' $InFile
... and got the same results.

Daniel B. Martin
 
1 members found this post helpful.
Old 12-23-2012, 01:31 PM   #5
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by danielbmartin View Post
I like this but don't understand what the %f bought. I coded it in fewer keystrokes this way ...
Code:
awk '{print("a="$1 " b="$2 " minus="$1-$2)}' $InFile
... and got the same results.

Daniel B. Martin
printf is a bit more flexible. It allows you to specify some details, such as precision. I also thought it was more readable, since it separates the data and the format. But of course, your version works ok, too.
 
  


Reply



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
How to display 2 different column field values as one column value in mysql VijayaRaghavanLakshman Linux - General 2 04-16-2012 09:56 AM
compare second column of a file then print the first column of it in a ne fil if true java_girl Linux - Newbie 2 03-16-2012 04:50 AM
how to sort the 2nd column on the basis of first column without repeating the value ? zediok Linux - Newbie 15 12-20-2011 11:48 AM
[SOLVED] Subtracting the value of a specific cell to the whole column udiubu Programming 13 11-11-2011 10:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:16 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration