regex - Replace all strings in file, using wildcards in the substitution -


i'm using sed in bash try replace strings matching:

compile 'com.test.*:*' 

with:

compile 'com.test.*:+' 

where * wildcard.

my file looks this, it's called moo.txt:

compile 'com.test.common:4.0.1' compile 'com.test.streaming:5.0.10' compile 'com.test.ui:1.0.7' 

and want this:

compile 'com.test.common:+' compile 'com.test.streaming:+' compile 'com.test.ui:+' 

i've tried use sed like:

sed -i -- "s/compile \'com.test.*:.*\'/compile \'com.test.*:+\'/g" moo.txt 

but makes file like:

compile 'com.test.*:+' compile 'com.test.*:+' compile 'com.test.*:+' 

any ideas how use wildcard in substitute field properly?

you matching things after com.test don't print them properly.

so indeed matching something, not printing back. instead, printing literal .*:

sed "s/compile \'com.test.*:.*\'/compile \'com.test.*:+\'/g" #                        ^^                        ^^ #                match                  print back? no 

to so, capture pattern , print using backreference.

sed -e "s/compile 'com.test(.*):.*'/compile 'com.test\1:+'/g" #                          ^^^^                      ^^ #                    catch             print back! yes! 

see repeating "compile..." much? means can extend capture beginning of line, since backreference print of back:

sed -e "s/^(compile 'com.test.*):.*'/\1:+'/g" #          ^^^^^^^^^^^^^^^^^^^^^     ^^ #           capture of      print 

note usage of -e allow sed capture groups (...). if didn't use -e, should \(...\).

note escaping single quotes, while not necessary since inside double quotes.


Comments