1838642 Members
2218 Online
110128 Solutions
New Discussion

Script command

 
SOLVED
Go to solution
Scott E Smith
Frequent Advisor

Script command

I have a script command that isues the follwing output.
(5%).

I need to strip the parens, the percent and the period from the output. The number may be a one, two or three digit number. Does anyone know how to remove the other characters? Thanks.
10 REPLIES 10
Patrick Wallek
Honored Contributor
Solution

Re: Script command

you can use sed in your script.

For example:

number=`echo $number | sed -e '1,$s/(//g'`
number=`echo $number | sed -e '1,$s/).//g'`

You would need both lines to get rid of the stuff on both sides of the number.
Curtis Larson
Trusted Contributor

Re: Script command

| tr -d "[(,),%,.]"
Curtis Larson
Trusted Contributor

Re: Script command

value="(5%)."
value=${value%%%*}
value=${value##\(*}
print $value
Dan Hetzel
Honored Contributor

Re: Script command

Hi Scott,

Here is a sed construct using regular expressions. It's a bit more general as it suppresses all non-digit characters:

echo $number | sed 's/[^[:digit:]]//g'

see man 5 regexp for details

Best regards,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Curtis Larson
Trusted Contributor

Re: Script command

oops,

value=${value##\(*}

should have been:

value=${value##*\(}
Scott E Smith
Frequent Advisor

Re: Script command

Thnak you all very much. The response is great.....
Shannon Petry
Honored Contributor

Re: Script command

First, your commands do not work Curtis. Even with your correction $value is still 5%).

Best bet is to use sed to strip the unwanted characters. Inside your script you can use 3 lines of sed to get rid of the known characters you do not want.
input="(5%)"
step1=`echo ${input} | sed -e '1,$s/(//g'`
step2=`echo ${step1} | sed -e '1,$s/)//g'`
output=`echo ${step2} | sed -e '1,$s/%//g'`
echo $output
5


Regards,
Shannon
Microsoft. When do you want a virus today?
Curtis Larson
Trusted Contributor

Re: Script command

Shannon,

sorry, the above didn't work for you. I guess I should have specified the shell.

it does work for ksh, for the posix shell you'll have to backslash the 3rd % character and if ksh was newer the 88 version you'd could have just done:

value=${value//[![:digit:]]/}
print $value

Shannon Petry
Honored Contributor

Re: Script command

Curtis,
thanks for clarifying. It does work in Korn Shell. I'll have to do more with korn one day......POSIX is global, so I use it all the time. Scripts in SCO, Linux, SunOS, HP-UX, AIX, and IRIX all work using the posix standard shell commands.........


Regards,
Shannon
Microsoft. When do you want a virus today?
Tommy Palo
Trusted Contributor

Re: Script command

Just some more variants:
| awk -F\( '{print $2}' | awk -F% '{print $1}'
or with sed (which BTW can take several "edits" as arguments, so no need to divide):
| sed -e 's:(::' -e 's:%.$::'

Keep it simple