Checking variable with specific string and stripping number from it.

I am parsing a file and I get differnt results everytime.
Sometimes I get 12s sometimes I get 54m and sometime 3h..

v1=12s or v1=54m or v1=3h

12s - 12 seconds
54m - 54 minutes
3h - 3 hour

I have to write a script in such a way that it whenever v1 is in minutes,
I should strip "m" from it.

if v1=32s ignore, if v1=54m then set v1=54, if v1=3h then ignore.

Any help would be appreciated. thx.
Without awk would be great.

$ a=50m
$ echo ${a/m/}
50
$ a=50s
$ echo ${a/m/}
50s
$ a=50h
$ echo ${a/m/}
50h

#echo $a
99m
DR# echo ${a/m/}
ksh: ${a/m/}: bad substitution

a=50m
echo ${a%m}
1 Like

It works. Thanks a lot.
I truly appreciate your help.

Regards,

Jay

---------- Post updated at 11:55 AM ---------- Previous update was at 11:45 AM ----------

One more question,
In my script sometimes value of variable test4 is empty.
because of that I get following.
##
ParseLog.sh[37]: 0.00%: more tokens expected
##

what can I do ?

Use

"$test4"

Suggestion did not work.

Can you provide me small script, with following info.

if $test value is empty or $test value is 12s( any numeric ) or $tet value is 32h ( any numeric instead of 32 ) , ignore.
If $test value is 15m ( any numeric ) I should do echo $(test%m).

your help would be highly appreciated. thx.

#/bin/sh
if [ -z "$test4" ]
then
   echo test4 is empty
else
   echo test4 is "$test4"
fi
test4=32m
test4=${test4%m}
if [ -z "$test4" ]
then
   echo test4 is empty
else
   echo test4 is "$test4"
fi

It does not help.
Loop is not working properly with -z option.

#/bin/sh
if [ "$test4" == "" ]
then
   echo test4 is empty
else
   echo test4 is "$test4"
fi
test4=32m
test4=${test4%m}
if [ "$test4" == "" ]
then
   echo test4 is empty
else
   echo test4 is "$test4"
fi

First part is good for comparing with empty value. Thx

Now I need to add the loop in which it should compare whether test4 ends "h" or "s", if it is then ignore.

Otherwise it should use test4 with $(test4%m).

${test4%m} will remove the m at the end leaving the h or s alone.

I agree the solution with echo removes "m" and keeps values whihc ends in "s" or "h".

My ultimate goal is to get value in numeric for "m".

But my main problem is I am not sure what I am going to get in test4.

So I have to compare whether it is "m" or "s" or "h"

Once it is with "m" then I can use echo command and go forward.

That's why I have to do compare with empty space, h or s.

Another example:

#/bin/sh
typeset -R1 lchar
test4=32m
lchar=$test4
echo test4 is $test4
echo lchar is $lchar
case $lchar in
  m)
    echo lchar is m
  ;;
  h|s)
    echo lchar is h or s
  ;;
  "")
    echo lchar is empty? how can this be?
  ;;
esac

I get this.
typeset: not found