Remove char if not a number | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Remove char if not a number

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-21-2013
millsy5 millsy5 is offline
Registered User
 
Join Date: Oct 2011
Last Activity: 13 November 2013, 4:22 AM EST
Posts: 44
Thanks: 19
Thanked 0 Times in 0 Posts
Remove char if not a number

I need to write a BASH script that takes a 2 character string and removes the second character if it is not a digit e.g.

If the string is numberical value >9 e.g. string1 = '34' then leave string1 = '34'.

However if the string is <10 e.g. string1 = '3X' then remove the second char (which can be any character, it's whatever was in memory) so that '3X' will now be '3'.

I was thinking about using ASCII values to determine whether the second character is a number.

Here's my attempt (I'm a newbie):


Code:
#!/bin/bash
 
if [ [string1[1]<'48'] || [string1[1]>'57'] ] # if 2nd char <0 or >9
then
        string1[1]="NOT SURE WHAT TO PUT HERE!!"
fi

Sponsored Links
    #2  
Old 08-21-2013
Ygor's Avatar
Ygor Ygor is offline Forum Advisor  
Advisor
 
Join Date: Oct 2003
Last Activity: 10 November 2014, 6:18 PM EST
Location: 54.23, -4.53
Posts: 1,801
Thanks: 1
Thanked 112 Times in 98 Posts
Try something like...
Code:
$ string1='3X'

$ case $string1 in [0-9][0-9]) : ;; [0-9]?) string1=${string1:0:1} ;; esac

$ echo $string1
3

$

The Following User Says Thank You to Ygor For This Useful Post:
millsy5 (08-21-2013)
Sponsored Links
    #3  
Old 08-21-2013
rbatte1 rbatte1 is offline Forum Staff  
Moderator
 
Join Date: Jun 2007
Last Activity: 20 November 2014, 11:55 AM EST
Location: Lancashire, UK
Posts: 1,899
Thanks: 606
Thanked 328 Times in 296 Posts
Would :-
Code:
echo "$string" | tr -d "[a-z][A-Z]"

... do the trick or does this not handle enough possible characters?

Perhaps:-
Code:
#!/bin/ksh
c1="${string%?}"                 # Get 1st character
c2="${string#?}"                 # Get 2nd character

c2=`echo ${c2}|egrep "[0-9]"`    # Get only numeric value or null for 2nd

string="${c1}${c2}"              # Stick string back together




I hope that this helps,
Robin
Liverpool/Blackburn
UK
The Following User Says Thank You to rbatte1 For This Useful Post:
millsy5 (08-21-2013)
    #4  
Old 08-21-2013
millsy5 millsy5 is offline
Registered User
 
Join Date: Oct 2011
Last Activity: 13 November 2013, 4:22 AM EST
Posts: 44
Thanks: 19
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Ygor View Post
Try something like...
Code:
$ string1='3X'
 
$ case $string1 in [0-9][0-9]) : ;; [0-9]?) string1=${string1:0:1} ;; esac
 
$ echo $string1
3
 
$

Thanks for the reply it worked perfectly.

However I don't fully understand it.
If the string is any 2 digits do nothing. That part is ok.
The next part, if the string is a number and any other char, then replace the string with ${string1:0:1}. Can you explain the string1:0:1 part please? Thanks
Sponsored Links
    #5  
Old 08-21-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 22 November 2014, 2:29 PM EST
Location: Aachen, Germany
Posts: 4,682
Thanks: 78
Thanked 1,158 Times in 1,088 Posts
read man bash on Parameter Expansion / Substring Expansion.
The Following User Says Thank You to RudiC For This Useful Post:
millsy5 (08-23-2013)
Sponsored Links
    #6  
Old 08-21-2013
neutronscott's Avatar
neutronscott neutronscott is offline Forum Advisor  
script kiddie
 
Join Date: Jun 2011
Last Activity: 16 November 2014, 10:02 PM EST
Location: McMurdo Station, Antarctica
Posts: 822
Thanks: 28
Thanked 245 Times in 231 Posts

Code:
$ string=3X; echo "${string//[!0-9]/}"
3

Sponsored Links
    #7  
Old 08-23-2013
millsy5 millsy5 is offline
Registered User
 
Join Date: Oct 2011
Last Activity: 13 November 2013, 4:22 AM EST
Posts: 44
Thanks: 19
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by RudiC View Post
read man bash on Parameter Expansion / Substring Expansion.

Code:
${parameter:offset:length}

That's exactly what I wanted but had no idea where to look.

Last edited by Franklin52; 08-23-2013 at 11:01 AM.. Reason: Please use code tags
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How count number of char? nimafire Shell Programming and Scripting 2 08-03-2013 08:16 PM
Help me to remove junk char Siddartha UNIX Desktop for Dummies Questions & Answers 2 06-20-2012 08:22 PM
Remove char after Colon gowrishankar05 Shell Programming and Scripting 3 04-18-2011 09:55 AM
can I remove the first char using AWK? Abdulelah Shell Programming and Scripting 2 01-03-2011 06:18 PM
how to get number char from a string netbanker Shell Programming and Scripting 4 07-16-2008 04:16 PM



All times are GMT -4. The time now is 03:16 AM.