Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SED with wildcard

Tags:

bash

sed

wildcard

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 
like image 505
mahmood Avatar asked Feb 08 '12 07:02

mahmood


2 Answers

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 
like image 104
tom Avatar answered Sep 19 '22 06:09

tom


So, the concept of a "wildcard" in Regular Expressions works a bit differently. In order to match "any character" you would use "." The "*" modifier means, match any number of times.

like image 33
Michael Manoochehri Avatar answered Sep 21 '22 06:09

Michael Manoochehri