Results 1 to 8 of 8

Thread: Korn/ksh - calculate percentage? (poss awk?)

  1. #1
    Theoretical Element Spud1's Avatar
    Join Date
    Jul 2003
    Location
    North West
    Posts
    7,508
    Thanks
    336
    Thanked
    320 times in 255 posts
    • Spud1's system
      • Motherboard:
      • Gigabyte Aorus Master
      • CPU:
      • 9900k
      • Memory:
      • 16GB GSkill Trident Z
      • Storage:
      • Lots.
      • Graphics card(s):
      • RTX3090
      • PSU:
      • 750w
      • Case:
      • BeQuiet Dark Base Pro rev.2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Asus PG35VQ
      • Internet:
      • 910/100mb Fibre

    Korn/ksh - calculate percentage? (poss awk?)

    Just a quick question,

    I've come accross a problem while writing a shell script (korn). Basically we don't have korn93 on most of our servers, so can't use the built-in floating point math to do things like work out percentages.

    That leaves me with finding another way - now from what I have found on the net I can use AWK to do this? Despite having done a fair bit of work with unix before I have not yet used AWK, and while i'm sure I should learn how to use it I don't have the time at the moment - so would anyone please be able to show me how to calculate a simple percentage using it?

    I have the values stored as variables in my script already, just need to evaluate them to a percentage now =)

    any help would be appreciated - a different solution or an explanation of how to use awk to do it would be great.

    cheers

    spud/pete


    edit: oh and also - this needs to be a reasonably standard unix component that I use, not perl for example (it's not set up on some of our servers) That's part of the problem, given the choice the whole script would have been done in perl lol
    Last edited by Spud1; 26-06-2006 at 05:03 PM.

  2. #2
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable
    Code:
    echo "${var1}/${var2}*100" | bc -l
    would something like that not do?..
    Last edited by aidanjt; 26-06-2006 at 07:02 PM.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  3. #3
    Theoretical Element Spud1's Avatar
    Join Date
    Jul 2003
    Location
    North West
    Posts
    7,508
    Thanks
    336
    Thanked
    320 times in 255 posts
    • Spud1's system
      • Motherboard:
      • Gigabyte Aorus Master
      • CPU:
      • 9900k
      • Memory:
      • 16GB GSkill Trident Z
      • Storage:
      • Lots.
      • Graphics card(s):
      • RTX3090
      • PSU:
      • 750w
      • Case:
      • BeQuiet Dark Base Pro rev.2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Asus PG35VQ
      • Internet:
      • 910/100mb Fibre
    because korn doesn't support floating point arithmatic by default

    Otherwise i could do as you suggest - but that only works for arithmatic.

    Later versions of ksh support doing TYPESET -F <varname> to mark it as floating point, but otherwise it will only do integer arithmatic (ie no decimals)

    btw in most unix OS's (this is Solaris 8) sh = ksh, if you look inside /bin/sh you just have a link to /bin/ksh =)
    Last edited by Spud1; 26-06-2006 at 07:05 PM.

  4. #4
    Theoretical Element Spud1's Avatar
    Join Date
    Jul 2003
    Location
    North West
    Posts
    7,508
    Thanks
    336
    Thanked
    320 times in 255 posts
    • Spud1's system
      • Motherboard:
      • Gigabyte Aorus Master
      • CPU:
      • 9900k
      • Memory:
      • 16GB GSkill Trident Z
      • Storage:
      • Lots.
      • Graphics card(s):
      • RTX3090
      • PSU:
      • 750w
      • Case:
      • BeQuiet Dark Base Pro rev.2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Asus PG35VQ
      • Internet:
      • 910/100mb Fibre
    well you edited your earlier post so my above reply doesn't make much sense anymore....

    But BC looks like it would do the job, wonder if it's faster than awk or not tho Still looking for an awk way to do it if anyone has one so I can compare the two, but i'll try BC tomorrow at work ty,

  5. #5
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable
    Quote Originally Posted by Spud1
    well you edited your earlier post so my above reply doesn't make much sense anymore....

    But BC looks like it would do the job, wonder if it's faster than awk or not tho Still looking for an awk way to do it if anyone has one so I can compare the two, but i'll try BC tomorrow at work ty,
    Yea, I reread your first post after I posted cos I'm half asleep and I missed that part about floating point math... sorry :/
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  6. #6
    mush-mushroom b0redom's Avatar
    Join Date
    Oct 2005
    Location
    Middlesex
    Posts
    3,510
    Thanks
    201
    Thanked
    388 times in 294 posts
    • b0redom's system
      • Motherboard:
      • Some iMac thingy
      • CPU:
      • 3.4Ghz Quad Core i7
      • Memory:
      • 24GB
      • Storage:
      • 3TB Fusion Drive
      • Graphics card(s):
      • nViidia GTX 680MX
      • PSU:
      • Some iMac thingy
      • Case:
      • Late 2012 pointlessly thin iMac enclosure
      • Operating System:
      • OSX 10.8 / Win 7 Pro
      • Monitor(s):
      • Dell 2713H
      • Internet:
      • Be+
    How accurate do you need to be? Can't you multiply by 100 before generating the percent ie:

    57/89

    Instead of doing (57/89) *100

    Try (57*100)/89

    The result will be the same, but you're less likely to hit FP problems?

    Alternativly, how about a little C++ program? If all you need to work out a percentage, you can take in 2 inputs and drop out a percent - assuming your architectures are consistant, you can deploy a little binary across everything?

    Tom

  7. #7
    Theoretical Element Spud1's Avatar
    Join Date
    Jul 2003
    Location
    North West
    Posts
    7,508
    Thanks
    336
    Thanked
    320 times in 255 posts
    • Spud1's system
      • Motherboard:
      • Gigabyte Aorus Master
      • CPU:
      • 9900k
      • Memory:
      • 16GB GSkill Trident Z
      • Storage:
      • Lots.
      • Graphics card(s):
      • RTX3090
      • PSU:
      • 750w
      • Case:
      • BeQuiet Dark Base Pro rev.2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Asus PG35VQ
      • Internet:
      • 910/100mb Fibre
    thanks aidanjt - BC seems to do the job nicely

    b0redom - thanks for the suggestion but BC is built in to all of our servers, so it's simplest to use that

  8. #8
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable
    Most welcome, it's nice to see people making good use of shell scripts, even if it's yucky solaris that they have to work with
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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