Using SED with wildcard

mahmood picture mahmood · Feb 8, 2012 · Viewed 99.2k times · Source

I want to replace a string with wildcard but it doesn't work.

The string looks like "some-string-8"

I wrote

sed -i 's/string-*/string-0/g' file.txt

but the output is

some-string-08

Answer

tom picture tom · Feb 8, 2012

The asterisk (*) means "zero or more of the previous item".

If you want to match any single character use

sed -i 's/string-./string-0/g' file.txt

If you want to match any string (i.e. any single character zero or more times) use

sed -i 's/string-.*/string-0/g' file.txt