Need help with bash script to isolate a portion of an output

Need help with bash script to isolate a portion of an output

Post by Mark De » Sun, 22 Aug 2004 04:16:31



Hey folks,

I have a script that has as its output the following text:

vmhba0:0:0:1         104856224  96367264   8488960  91%
/vmfs/vmhba0:0:0:1

What I need is a way to isolate the number before the % (91% in this
example) and put some logic around that simply says 'if the number is
more than 90 do this else nothing'. I can handle the logic but having
a problem isolating the number. I've tried cut and it works except
that cut treats the spaces as actual spaces not tabs and that will
vary with the other numbers (for instance, I can do a  vdf | grep
vmhba2:0:0:8 | cut -d" " -f19 and that will isolate the 59% but
another line will need the -f to be 18 or something else). If anyone
knows how to tell cut to use white space instead of actual spaces or a
way to always be consistent the problem is solved. I'm probably
looking right at the solution but getting a mental block.

Thanks in advance.

 
 
 

Need help with bash script to isolate a portion of an output

Post by Bit Twiste » Sun, 22 Aug 2004 04:22:39



Quote:> Hey folks,

> I have a script that has as its output the following text:

> vmhba0:0:0:1         104856224  96367264   8488960  91%
> /vmfs/vmhba0:0:0:1

Since you did not show any of you code here is sometning to play with.

aa="vmhba0:0:0:1  104856224  96367264   8488960  91% /vmfs/vmhba0:0:0:1"
set - $aa
echo $5

 
 
 

Need help with bash script to isolate a portion of an output

Post by William Par » Sun, 22 Aug 2004 04:49:19



> Hey folks,

> I have a script that has as its output the following text:

> vmhba0:0:0:1         104856224  96367264   8488960  91%
> /vmfs/vmhba0:0:0:1

> What I need is a way to isolate the number before the % (91% in this
> example) and put some logic around that simply says 'if the number is
> more than 90 do this else nothing'. I can handle the logic but having
> a problem isolating the number. I've tried cut and it works except
> that cut treats the spaces as actual spaces not tabs and that will
> vary with the other numbers (for instance, I can do a  vdf | grep
> vmhba2:0:0:8 | cut -d" " -f19 and that will isolate the 59% but
> another line will need the -f to be 18 or something else). If anyone
> knows how to tell cut to use white space instead of actual spaces or a
> way to always be consistent the problem is solved. I'm probably
> looking right at the solution but getting a mental block.

> Thanks in advance.

To get '91%',
    1.  x='... 91% ...'
        set $x
        echo $5

    2.  echo $x | awk '{print $5}'

    3.  read a b c d e <<< "$x"
        echo $e

Then, to get '91',
    1.  tr -d '%'

    2.  sed 's/%//'

    3.  y='91%'
        echo ${y%\%}

Or, if you're adventurous, do the whole in one-step,
    1.  awk -F '[ %]+' '{print $5}'

    2.  sscanf " %s %s %s %s %[0-9]%% " a b c d e
        echo $e

        Ref: http://freshmeat.net/projects/bashdiff/
             help sscanf

--

Open Geometry Consulting, Toronto, Canada

 
 
 

Need help with bash script to isolate a portion of an output

Post by Chris F.A. Johnso » Sun, 22 Aug 2004 05:13:46



Quote:> Hey folks,

> I have a script that has as its output the following text:

> vmhba0:0:0:1         104856224  96367264   8488960  91% /vmfs/vmhba0:0:0:1

> What I need is a way to isolate the number before the % (91% in this
> example) and put some logic around that simply says 'if the number is
> more than 90 do this else nothing'. I can handle the logic but having
> a problem isolating the number. I've tried cut and it works except
> that cut treats the spaces as actual spaces not tabs and that will
> vary with the other numbers (for instance, I can do a  vdf | grep
> vmhba2:0:0:8 | cut -d" " -f19 and that will isolate the 59% but
> another line will need the -f to be 18 or something else). If anyone
> knows how to tell cut to use white space instead of actual spaces or a
> way to always be consistent the problem is solved. I'm probably
> looking right at the solution but getting a mental block.

script | { read a b c d e f
  if [ ${e%%} -gt 90 ]
  then
     : do something
  else
     : do something else
  fi

Quote:}

--
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
 
 
 

1. BASH BASH BASH BASH BASH BASH BASH BASH BASH BASH

Is there a proper fixed bash on any of the FTP sites out there?

I know there bash is on the usual sites but I don't know if they are
bugged or not :(

Regards,

Neil.

--


------------------------------------| Edinburgh, EH14 2DE, United Kingdom
**Domino: There`s nothing you can do when you`re the next in line: Domino**

2. Top 10 subjects comp.unix.questions

3. Passing option to bash script help needed

4. httpd.conf servername overiding virtualhost setting of the same

5. Need help wirh scripting arrays in bash

6. External, Parallel Port Hard Drive

7. Need help on bash scripting

8. PCI driver for the NE3200 card

9. bash script, need help ! Please!

10. Keyboard shortcuts with Bash script -- Need HELP

11. Need help with an IF in a bash script

12. Help needed bash scripting

13. Newbie needs help with a bash script