Checking variable with specific string and stripping number from it.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking variable with specific string and stripping number from it.
# 1  
Old 10-17-2012
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.
# 2  
Old 10-17-2012
Code:
$ a=50m
$ echo ${a/m/}
50
$ a=50s
$ echo ${a/m/}
50s
$ a=50h
$ echo ${a/m/}
50h

# 3  
Old 10-17-2012
#echo $a
99m
DR# echo ${a/m/}
ksh: ${a/m/}: bad substitution
# 4  
Old 10-17-2012
Code:
a=50m
echo ${a%m}

This User Gave Thanks to rdrtx1 For This Post:
# 5  
Old 10-17-2012
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 ----------

Quote:
Originally Posted by rdrtx1
Code:
a=50m
echo ${a%m}

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 ?
# 6  
Old 10-17-2012
Use
Code:
"$test4"

# 7  
Old 10-17-2012
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding variable number of space in between two string

Hi, I am looking for the way to add variable number of spaces between two string. e.g input line is a ,bb abc ,bcb pqr ,bfg My output should be something like this a ,bb abc ,bcb pqr ,bfg This text... (9 Replies)
Discussion started by: diehard
9 Replies

2. Shell Programming and Scripting

Stripping characters from a variable

I'm using a shell script to get user input with this command: read UserInput I would then like to take the "UserInput" variable and strip out all of the following characters, regardless of where they appear in the variable or how many occurrences there are: \/":|<>+=;,?*@ I'm not sure... (5 Replies)
Discussion started by: nrogers64
5 Replies

3. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

4. Shell Programming and Scripting

stripping a variable down

I have a variable that has an absolute path for a file on my computer. This dynamically changes. Is there a way I can assign two new variables from that one? variable: /Users/keith/Desktop/test/file.mov 1) filename - no path or extention ....so just....file 2) path no filename or... (3 Replies)
Discussion started by: mainegate
3 Replies

5. Shell Programming and Scripting

How to extract variable number from a String

hi,I am new to shell script,I have String,like this: Number of rows exported: 5321 the numbe at end could changing,how can I extract this number and assign it to a variable,then use it later in script. thanks. (19 Replies)
Discussion started by: vitesse
19 Replies

6. Shell Programming and Scripting

[csh] checking for specific character ranges in a variable

I want to check if a zip code is valid, using a variable that stores the zipcode. I am not sure how I would do this in a script. I know that simply checking for the numerical range of the number will not work, because '1' would be '00001' in zip code format. I know when I am in shell, I can use... (5 Replies)
Discussion started by: userix
5 Replies

7. Shell Programming and Scripting

Stripping the spaces in a string variable

Hi , i have to strip the spaces in the string which has the following value ABC DEF i want this to appear like this ABC DEF is there any spilt method? please help.... Thanks (3 Replies)
Discussion started by: rag84dec
3 Replies

8. UNIX for Dummies Questions & Answers

Stripping a portion of string from behind!!!

Hi, How to strip a portion of a file name from behind...Say for Eg..i have a file name like aaaaa.bbbbb.Mar-17-2007 i want to remove .Mar-17-2007...is there a one line command which can give this output... Thanks Kumar (5 Replies)
Discussion started by: kumarsaravana_s
5 Replies

9. UNIX for Dummies Questions & Answers

check whether variable number or string

I am passing an argument to a file and i wanna check whether the argument is a number or string ? how can i do this? (4 Replies)
Discussion started by: rolex.mp
4 Replies

10. Shell Programming and Scripting

Number of specific char in a string.

I wish to compute the number of dot chars in a string. Example: VAR="aaaa.bbbbb.cccc" I try the shortest command to solve this test. Thanks in advance for your help. Regards, Giovanni (7 Replies)
Discussion started by: gio123bg
7 Replies
Login or Register to Ask a Question