Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page EXPR the value of 2 files
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 1 Week Ago   #1 (permalink)
markbusu
Just Joined!
 
Join Date: May 2008
Posts: 14
Question EXPR the value of 2 files

Hi guys,

Having some trouble finding a method to perform a calculation between the contents of two files... For example

Say i have:

File 1

-------------
1
23
54343
123
45676
8855
-------------

File 2

-------------
9087
576
7897
4778
665
4545
-------------

I would like to add or divide the contents of the two files between each other, such as:

1*9087
23*576
54343*7897
123*4778
45676*665
8855*4545

and finally output the result to another file...

How am I able to add the contents of these files?

Note: The number of lines between each file is always the same...

Thanks for any help you can offer!!

Mark
markbusu is offline   Reply With Quote
Old 1 Week Ago   #2 (permalink)
radoulov
Just Joined!
 
radoulov's Avatar
 
Join Date: Sep 2007
Posts: 68
With zsh:

Code:
printf "%d\n" $(paste -d\* file1 file2)>final
This should work with other shells:

Code:
printf "%s\n" $(paste -d\* file1 file2)|bc>final

Or just use awk:

Code:
awk 'NR==FNR{_[FNR]=$0;next}$0=$0*_[FNR]' file1 file2>final
Or:

Code:
eval printf "%s\\\n" $(printf '$((%s)) ' $(paste -d\* file1 file2))
P.S. Post modified ...
radoulov is offline   Reply With Quote
Old 1 Week Ago   #3 (permalink)
wje_lf
Linux Engineer
 
wje_lf's Avatar
 
Join Date: Sep 2007
Location: Mariposa
Posts: 757
I like the second alternative:
Code:
printf "%s\n" $(paste -d\* file1 file2)|bc>final
but it can be cleaned up thus:
Code:
paste -d\* file1 file2 | bc -q > final
__________________
--
Bill

Old age and treachery will overcome youth and skill.
wje_lf is offline   Reply With Quote
Old 1 Week Ago   #4 (permalink)
radoulov
Just Joined!
 
radoulov's Avatar
 
Join Date: Sep 2007
Posts: 68
Absolutely right
I didn't realize that I was in zsh when I posted this:

Code:
printf "%d\n" $(paste -d\* file1 file2)
And after that I tried to give more portable solution in a hurry
radoulov is offline   Reply With Quote
Old 1 Week Ago   #5 (permalink)
markbusu
Just Joined!
 
Join Date: May 2008
Posts: 14
Thanks for the tips guys!!

I tried using the cleaned version, however i dont get any output back...

Im using Bash... and i get the following output:

# ./sed.sh
$ paste -d\* c.txt d.txt | bc -q > final
+ paste '-d*' c.txt d.txt
+ bc -q

Do you know what might be the cause of this?

Thanks!
markbusu is offline   Reply With Quote
Old 1 Week Ago   #6 (permalink)
radoulov
Just Joined!
 
radoulov's Avatar
 
Join Date: Sep 2007
Posts: 68
What's the output of:

Code:
cat final
radoulov is offline   Reply With Quote
Old 1 Week Ago   #7 (permalink)
markbusu
Just Joined!
 
Join Date: May 2008
Posts: 14
Perfect! worked like a charm...

Thanks m8!
markbusu is offline   Reply With Quote
Old 1 Week Ago   #8 (permalink)
markbusu
Just Joined!
 
Join Date: May 2008
Posts: 14
Hi again

So i needed to make a change, rather then a multiplication, i would need to perform a division... such as:

paste -d\/ file1 file2 | bc -q > final

However, the problem that the file final only uses whole numbers... however im performing a division on ms... such as 0.0000445..

Is there any way of getting a number to 10 decimal points?

Thanks!
markbusu is offline   Reply With Quote
Old 1 Week Ago   #9 (permalink)
radoulov
Just Joined!
 
radoulov's Avatar
 
Join Date: Sep 2007
Posts: 68
Code:
bc<<! >final
scale=10
$(paste -d\/ file1 file2)
!
If you want a leading 0:

Code:
printf "%.10f\n" $(paste -d\/ file1 file2|bc -l)>final
radoulov is offline   Reply With Quote
Old 6 Days Ago   #10 (permalink)
markbusu
Just Joined!
 
Join Date: May 2008
Posts: 14
Thanks once again... that did the trick...

Ok im nearly ready here... however encountered another issue...

Basically im parsing a document which contains SQL Statements that were run... calculating the number of times a SQL Statement was run agianst the total execution time...

However, when i encounter an issue were the SQL Statement times run is 0 and the total execution time is 0... i.e 0 / 0 generats the following error:

Runtime error (func=(main), adr=11): Divide by zero.

The error is generated by bc. Is there a method of handling this exception, therefore should the division be 0/0... You return the result 0?

Thanks once agani guys!
markbusu is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


All times are GMT. The time now is 03:52 AM.

Powered by vBulletin 3.6.8 ©2000 - 2007, content relevant URLs by vBSEO, Property of Core Root.

Content Relevant URLs by vBSEO 3.0.0