Grep on unix / linux: how to replace or capture text?

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 text

In PHP, I could just do something like this:

preg_match ('# (word1). * (word2) #', $ text, $ matches);

I'm not sure how this is possible in * nix ...

+5
source share
3 answers

Part 1

, sed g, , g .

$ echo "my #1 example!" | sed s/[^a-zA-Z0-9\_]//g
my1example
$

bash: :

$ text="my #1 example!"

# non-global replacement. Only the space is delete.
$ echo ${text/[^a-zA-Z0-9_]/''}
my#1 example!

# global replacement by adding an additional / 
$ echo ${text//[^a-zA-Z0-9_]/''}
my1example

2

sed, PHP: :

# swap foo and bar number using capturing and back reference.
$ echo 'foo1 bar2' | sed -r 's/foo([0-9]+) bar([0-9]+)/foo\2 bar\1/'
foo2 bar1
$ 
+14

codaddict sed tr .

echo "my #1 _ example!" | tr -d -C '[[:alnum:]_]'

[:alnum:], .

+1

, regex bash?

$ text="my #1 example!"
$ echo ${text//[^a-zA-Z0-9_]/}
my1example

// 1 .

, bash 3.2 ++

$ [[ $text =~ "(my).*(example)" ]]
$ echo ${BASH_REMATCH[1]}
my
$ echo ${BASH_REMATCH[2]}
example
0

All Articles