Find the answer to your Linux question:
Results 1 to 4 of 4
Hello! I am new to bash programming, and I have messed around with integers and matrices, but I am curious if doubles can be used. lets say I have a ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2014
    Posts
    1

    Bash(awk) - comparing decimal values from file


    Hello! I am new to bash programming, and I have messed around with integers and matrices, but I am curious if doubles can be used. lets say I have a file like...

    14.23 15.10 10.90

    Now I want to find the smallest DECIMAL value of these and print it out. So 15.10 would be the printout. Can I write a bash script for this? I would like to use awk because I feel it is the easiest.

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    hi,

    what if an equality happens ?
    e.g. :
    Code:
    25.35 12.10 65.10 34.24

  3. #3
    Just Joined! mrbruno's Avatar
    Join Date
    Jan 2013
    Location
    /MilkyWay/Sol/Earth/USA/NC/Raleigh
    Posts
    63
    Quote Originally Posted by Tree55Topz View Post
    Hello! I am new to bash programming, and I have messed around with integers and matrices, but I am curious if doubles can be used. lets say I have a file like...

    14.23 15.10 10.90

    Now I want to find the smallest DECIMAL value of these and print it out. So 15.10 would be the printout. Can I write a bash script for this? I would like to use awk because I feel it is the easiest.
    How about this:

    Code:
    $ cat deccomp
    echo 14.23 15.10 10.90 | awk '
    function trunc(x) {
      return x - int(x)
    }
    
    {
      if (NF > 0) {
        smallest = $1
        for (i = 2; i<=NF; i++) {
          curr = trunc($i)
          if (trunc(smallest) > curr)
            smallest = $i
        }
        print smallest
      }
    }'
    $ sh deccomp
    15.10
    $
    I figure this probably has problems with negatives but they can be handled pretty easily by finding the absolute value of the return value in the function.

  4. #4
    Just Joined! mrbruno's Avatar
    Join Date
    Jan 2013
    Location
    /MilkyWay/Sol/Earth/USA/NC/Raleigh
    Posts
    63
    Quote Originally Posted by watael View Post
    hi,

    what if an equality happens ?
    e.g. :
    Code:
    25.35 12.10 65.10 34.24
    That's a good question. I suppose it depends on the purpose.

Posting Permissions

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