Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I want to strip all numeric characters from a String variable, eg from:

VARIABLE=qwe123rty567

to:

echo $VARIABLE
> qwerty

I've search many posts but they either use sed to output to file/file names, or output to echo. I was not able to get it working with because of the white space:

VARIABLE=$VARIABLE | sed 's/[0-9]*//g'
share|improve this question

4 Answers 4

up vote 5 down vote accepted

With bash:

$ printf '%s\n' "${VARIABLE//[[:digit:]]/}"
qwerty

[:digit:] can contain other characters than 0 to 9 depend on your locale. If you want only to remove 0 to 9, use C locale instead.

share|improve this answer
1  
Note: depending on your locale, [:digit:] might contain more or less than you would expect it to. If you only want the numbers 0 to 9, use LC_COLLATE=C and [0-9]. – Chris Down 20 hours ago
1  
@ChrisDown: Yes, the question asked for numeric characters, not 0 to 9, so I used [:digit:] instead of [0-9]. – cuonglm 19 hours ago
    
Yeah, it's good for the question asked. It's not uncommon for Westerners to assume that [;digit:] == [0-9] though, so worth mentioning the pitfalls :-) – Chris Down 16 hours ago
VARIABLE=qwe123rty567
IFS=0123456789
set -f # Disable glob
printf %s $VARIABLE

qwerty

further manipulation is possible.

VARIABLE=qwe123rty567
IFS=0123456789
set -f # Disable glob
set -- $VARIABLE
IFS=;   VARIABLE=$*
printf "replaced $# numbers in \$VARIABLE. RESULT:\t%s\n" "$*"

replaced 6 numbers in $VARIABLE. RESULT:    qwerty
share|improve this answer
    
hmm, my variable becomes "qwe rty" with 3 spaces in the middle and 3 at the end – George 20 hours ago
    
@George - you're probably using echo which replaces nulls with spaces - like eval. – mikeserv 20 hours ago

White space (actually, failure to quote your variables) was only part of the problem.

You can't just pipe a variable through sed like that, it doesn't work. More precisely, it doesn't pipe the value of "$VARIABLE" through sed, the shell will try to execute the value of "$VARIABLE" and pipe the output of that through sed. BTW, this is not a bug - this is useful if $VARIABLE happens to contain a valid command like ls or rsync or whatever.

Also, if you want to assign the ouput of a command or pipeline to a variable, you need to surround that command/pipeline with $().

So, to modify a variable with sed, you need to do something like this:

VARIABLE=$(printf '%s' "$VARIABLE" | sed 's/[0-9]*//g')

You could use echo there instead of printf but echo will interpret and act on certain character sequences in $VARIABLE (e.g. \t, \n, \r, etc), while printf won't. You'll run across a lot of examples using echo...replace them with printf '%s', it's much safer.

share|improve this answer
    
Is there any real reason to single quote the %s ? – Wildcard 8 hours ago
1  
@wildcard. %s by itself, no. more complicated format strings should be quoted, though, so it's a good habit to get into. – cas 8 hours ago
    
there was no failure to quote any variables - everything was quoted just fine. and white-space isn't even involved (and rarely actually is). i know the asker mentions it as a possible source of whatever the issue was, but it probably isn't helpful to encourage wrongful assumptions. your command substitution will strip white-space from the tail of whatever is in $VARIABLE, though. – mikeserv 3 hours ago
    
and you're making that judgement on one made up example of VARIABLE=qwe123rty567? OK, right. I made my assessment ("failure to quote your variables") on the basis of none of the OP's examples having quotes around the variables, and the fact that he mentioned white-space as being a problem. – cas 2 hours ago

For variety, here's methods using

tr:

VARIABLE=$(printf '%s' "$VARIABLE" | tr -d '0123456789')

sed:

VARIABLE=$(printf '%s' "$VARIABLE" | sed 's/[0-9]//g')

Bash expansion, by far the most terse:

VARIABLE=${VARIABLE//[0-9]/}

and finally Bash expansion again, this time using the [[:digit:]] character class.

VARIABLE=${VARIABLE//[[:digit:]]/} 

Note that (as others have pointed out) [[:digit:]], should cover anything defined as a digit in your locale.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.