So, I'm pretty good with regular expressions, but I have problems with them in unix. Here are two things I would like to know how to do:
1) Replace all text except letters, numbers and underscores
In PHP, I would do this: (works great)
preg_replace ('# [^ a-zA-Z0-9 _] #', '', $ text).
In bash, I tried this (with limited success); it doesn't seem to allow you to use the full set of regular expressions:
text = "my # 1 example!"
$ {text / [^ a-zA-Z0-9 _] / '')
I tried it with sed, but it still has problems with a full set of regular expressions:
echo "my # 1 example!" | sed s / [^ a-zA-Z0-9 \ _] //
I'm sure there is a way to do this with grep too, but he tried to split it into several lines:
echo abc \! \ @ \ # \ $ \% \ ^ \ & \ * \ (222 | grep -Eos '[a-zA-Z0-9 \ _] +'
And finally, I also tried using expr , but it looks like it had very limited support for extended regular expression ...
2) Capture (several) parts of the textIn PHP, I could just do something like this:
preg_match ('# (word1). * (word2) #', $ text, $ matches);
I'm not sure how this is possible in * nix ...