Doing calculations with bc on one field


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Doing calculations with bc on one field
# 1  
Old 12-31-2011
Doing calculations with bc on one field

Hello,

I have to turn:
Code:
Apple Inc.:325,64:329,57

into
Code:
Apple Inc.:325,64:329,57:3,93

3,93=329,57-325,64.

My code:
Code:
cat beurs.txt | sed 's/\([^:]*\):\([0-9]*\),*\([0-9]*\):\([0-9]*\),\([0-9]*\)/\4\.\5-\2\.\3/' beurs.txt | bc| tr '.' ',' | sed 's/^-*,/0,/' > winstmarges.txt; paste -d: beurs.txt winstmarges.txt; rm winstmarges.txt

works but can I do it without creating temporary files?
Can I use bc to do calculations on a specific field?
Or can I do calculations within sed?

Thanks
# 2  
Old 12-31-2011
Try this...
Code:
awk -F: '{x=$0;gsub(",",".");y=$3-$2;gsub("\\.",",",y); print x OFS y }' OFS=: infile

--ahamed

Last edited by ahamed101; 01-01-2012 at 08:12 AM..
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 01-01-2012
If you want (slash need) to stick to sed and bc, try this:

Code:
sed 's/^\(.*:\)\([0-9]*\),*\([0-9]*\):\([0-9]*\),\([0-9]*\)/print \"\1\2,\3:\4,\5:\",\4\.\5-\2\.\3,\"\n\"/' beurs.txt|bc|sed 's/\(\.\([0-9]*\)\)$/,\2/'

Explanation: Don't abuse a cat! First turn the line into a string by enclosing it in '"'. Add a comma, the expression, a comma and a newline. Feed this to the print function of bc.

Finally translate the last '.' into a ',', because bc is not aware of locale-issues. In the Netherlands we use a decimal ',' instead of a '.'.

When nesting groups in sed, the deeper nested groups get higher numbers. e.g.: (a(b(c))) \1=abc, \2=bc, \3=c. This way we can get rid of the '.'. Of course this applies not only to sed but to other regular expression environments as well.

I've tested this on Ubuntu 11.10.
Quote:
bash --version
GNU bash, versie 4.2.10(1)-release (x86_64-pc-linux-gnu)
sed --version
GNU sed versie 4.2.1
bc --version
bc 1.06.95
Personally I think the above solution is very unreadable. A fairly simple way to achieve your goal with awk could be:
Code:
awk -F: '{printf $0":";gsub(",",".");wm=$3-$2;sub("[.]",",",wm);print wm}' beurs.txt

If this doesn't work in your situation try nawk instead of awk.

Groetjes/Greetings

Eric
This User Gave Thanks to edehont For This Post:
# 4  
Old 01-01-2012
Hello,
Thank you!
Yes, for now, I'd like to stick with sed.
So when you have a line with a string in it to calculate (example: test,5.5+5.5)
than you have to enclose the string into "" followed by a comma, the expression you want to calculate, another comma and a newline? (print "test",5.5+5.5,"\n")

I'm from Belgium, so we also use a decimal ',' instead of a '.'.

Greets!
PS: Happy New year!

---------- Post updated at 04:58 PM ---------- Previous update was at 12:00 PM ----------

And what if you have more than one string?
Example:
Code:
test:1+3:unix:1+5

to
Code:
test:4:unix:6

# 5  
Old 01-01-2012
To be honest, I've hardly ever worked with bc before. I just took a glimpse at the man page and came up with this idea. Since we are mixing strings and expressions, the print command seemed the way to go. It prints strings while other things in the argument list are interpreted as expressions before they get printed.

Your latest exercise could be solved as follows:
Code:
echo 'test:1+3:unix:1+5'|sed 's/^\(.*:\)\(.*\)\(:.*:\)\(.*\)$/print \"\1\",\2\,\"\3\",\4,\"\n\"/'|bc

The result is:
Code:
test:4:unix:6

Groetjes/greetings,
Eric
This User Gave Thanks to edehont For This Post:
# 6  
Old 01-01-2012
Thank you so much!
I'll give it a try tomorrow.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Loop doing calculations

Hello. I'm writing an awk script that looks at a .csv file and calculates the weighted grade for each student based on the scores and categories in the file. I am able to get the script to run the only issue however is that the same score for each student is the same. I'm self-teaching myself the... (1 Reply)
Discussion started by: Eric7giants
1 Replies

2. Shell Programming and Scripting

Output calculations

Attached are the is original output (zipped file) and a custom file using the awk code below in which the average reads per bait are calculated (average.txt) awk '{if(len==0){last=$4;total=$6;len=1;getline}if($4!=last){printf("%s\t%f\n", last,... (7 Replies)
Discussion started by: cmccabe
7 Replies

3. Shell Programming and Scripting

Number calculations

I'm writing a script that will read all the fields of a text file into an array(if they are numeric), while at the same time computing the minimum and maximum values from the file. After that I want to output the average of all the numbers in the array. The first problem I'm having is that many... (10 Replies)
Discussion started by: ksmarine1980
10 Replies

4. Shell Programming and Scripting

Problem with calculations

grep Quality abc.txt | awk -F"=" '{print $2}' o/p is given as 70/70 49/70 I want in the below format (percentage format) 100% 70% help me!!!!:confused::confused::confused: ---------- Post updated at 09:59 AM ---------- Previous update was at 09:57 AM ---------- Cell 01 -... (3 Replies)
Discussion started by: nikhil jain
3 Replies

5. UNIX for Dummies Questions & Answers

help with doing calculations on data

Dear All, I have a long list like this: 337 375 364 389 443 578 1001 20100 . . . . etc I would like to substract each value from the first entry which in this case is 337 and report it in a separate column. So the expected output looks like 337 0 (10 Replies)
Discussion started by: pawannoel
10 Replies

6. Shell Programming and Scripting

calculations in bash

HI i have following problem, i need to use split command to split files each should be cca 700 lines but i dont know how to inplement it in the scripts becasuse each time the origin file will be various size , any body got any idea cheers (2 Replies)
Discussion started by: kvok
2 Replies

7. UNIX for Dummies Questions & Answers

Date Calculations

I need to be able to use the current date and calculate 7 days ago to be stored in another variable to be passed to a file in my Unix shell script. I need the date in the following format: date '+%m/%d/%Y' or 05/16/2006 How do I calculate date minus 7 days or 1 week ago? (8 Replies)
Discussion started by: mitschcg
8 Replies

8. Shell Programming and Scripting

ksh, calculations using bc

hi all, was wondering if there is another way to do calculations in ksh scripts other than using bc ?? i am using a script to calculate average response time and my script errors out after running for a bit. e.g code i am using : averageTime=$(print "$totalTime / $numberOfEntries" |... (2 Replies)
Discussion started by: cesarNZ
2 Replies

9. UNIX for Dummies Questions & Answers

Time Calculations

I'm trying to have a loop print out statistics every X number of seconds. How can I add a specific number of seconds to a time variable and make a comparison? Thanks ahead of time. For example: startTime = `date +%H%M%S` currentTime = $startTime executeTime = startTime + X # X is equal... (5 Replies)
Discussion started by: Nysif Steve
5 Replies

10. UNIX for Dummies Questions & Answers

Float calculations

As expr is used for integer calculations, which command is used for float calculations. (1 Reply)
Discussion started by: sharmavr
1 Replies
Login or Register to Ask a Question