ajilesh
December 12, 2008, 6:52am
1
Hi All,
I wanted to delete all the unwanted characters in the string. ie, to delete all the characters which are not alpha numeric values.
var1="a./bc"
var2='abc/\."123'
like to get the output as
print var1
abc
print var2
abc123
Could you guys help me out pls.
Your help is appreciated.
Thanks in advance.
zaxxon
December 12, 2008, 7:27am
2
With tr for example:
echo 'abc/\."123'| tr -d './\\"'
abc123
otheus
December 12, 2008, 1:37pm
3
You have to something like this:
var=<something strange>
var=`echo $var | tr -dc 'a-zA-Z0-9'`
The -c option complements the character set (a-zA-Z0-9) and the -d removes all matching characters. The backtic operators read the output back into the expression so that var again has the new value.
ajilesh:
Hi All,
I wanted to delete all the unwanted characters in the string. ie, to delete all the characters which are not alpha numeric values.
var1="a./bc"
var2='abc/\."123'
like to get the output as
print var1
abc
print var2
abc123
In bash or ksh93, to remove non-alphanumeric characters:
printf "%s\n" "${var1//[!a-zA-Z0-9]/}"