Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 16
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 ...
  1. #1
    Just Joined!
    Join Date
    May 2008
    Posts
    18

    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

  2. #2
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    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 ...

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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.

  4. #4
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    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

  5. #5
    Just Joined!
    Join Date
    May 2008
    Posts
    18
    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!

  6. #6
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    What's the output of:

    Code:
    cat final

  7. #7
    Just Joined!
    Join Date
    May 2008
    Posts
    18
    Perfect! worked like a charm...

    Thanks m8!

  8. #8
    Just Joined!
    Join Date
    May 2008
    Posts
    18
    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!

  9. #9
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Code:
    bc<<! >final
    scale=10
    $(paste -d\/ file1 file2)
    !
    If you want a leading 0:

    Code:
    printf "&#37;.10f\n" $(paste -d\/ file1 file2|bc -l)>final

  10. #10
    Just Joined!
    Join Date
    May 2008
    Posts
    18
    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!

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...