Search and replace in bash using regular expressions
Use sed:
MYVAR=ho02123ware38384you443d34o3434ingtod38384dayecho "$MYVAR" | sed -e 's/[a-zA-Z]/X/g' -e 's/[0-9]/N/g'# prints XXNNNNNXXXXNNNNNXXXNNNXNNXNNNNXXXXXXNNNNNXXX
Note that the subsequent -e
's are processed in order. Also, the g
flag for the expression will match all occurrences in the input.
You can also pick your favorite tool using this method, i.e. perl, awk, e.g.:
echo "$MYVAR" | perl -pe 's/[a-zA-Z]/X/g and s/[0-9]/N/g'
This may allow you to do more creative matches... For example, in the snip above, the numeric replacement would not be used unless there was a match on the first expression (due to lazy and
evaluation). And of course, you have the full language support of Perl to do your bidding...
These examples also work in bash no need to use sed:
#!/bin/bashMYVAR=ho02123ware38384you443d34o3434ingtod38384dayMYVAR=${MYVAR//[a-zA-Z]/X} echo ${MYVAR//[0-9]/N}
you can also use the character class bracket expressions
#!/bin/bashMYVAR=ho02123ware38384you443d34o3434ingtod38384dayMYVAR=${MYVAR//[[:alpha:]]/X} echo ${MYVAR//[[:digit:]]/N}
output
XXNNNNNXXXXNNNNNXXXNNNXNNXNNNNXXXXXXNNNNNXXX
What @Lanaru wanted to know however, if I understand the question correctly, is why the "full" or PCRE extensions \s\S\w\W\d\D
etc don't work as supported in php ruby python etc. These extensions are from Perl-compatible regular expressions (PCRE) and may not be compatible with other forms of shell based regular expressions.
These don't work:
#!/bin/bashhello=ho02123ware38384you443d34o3434ingtod38384dayecho ${hello//\d/}#!/bin/bashhello=ho02123ware38384you443d34o3434ingtod38384dayecho $hello | sed 's/\d//g'
output with all literal "d" characters removed
ho02123ware38384you44334o3434ingto38384ay
but the following does work as expected
#!/bin/bashhello=ho02123ware38384you443d34o3434ingtod38384dayecho $hello | perl -pe 's/\d//g'
output
howareyoudoingtodday
Hope that clarifies things a bit more but if you are not confused yet why don't you try this on Mac OS X which has the REG_ENHANCED flag enabled:
#!/bin/bashMYVAR=ho02123ware38384you443d34o3434ingtod38384day;echo $MYVAR | grep -o -E '\d'
On most flavours of *nix you will only see the following output:
ddd
nJoy!