You are using (foo)
which is mean to capture the string foo
. However, by default, sed
will read that as a literal (
followed by foo
and another )
and since you don't have any (
or )
in your input, that will never match.
For sed
to use capturing parentheses, you need t escape them (\(foo\)
) or you need to use -E
. You don't actually need this at all here though because you are never referring to the thing you were trying to capture. All you need is:
$ sed 's/shared:core:.*/shared:core:1.1.1/' file
shared:core:1.1.1
Or even:
$ sed -E 's/(shared:core:).*/\1:1.1.1/' file
shared:core::1.1.1
Or even simpler:
$ sed -E 's/(.*):.*/\1:1.1.1/' file
shared:core:1.1.1
Now, if your input is more complicated and what you are trying to match is 1 to 5 repetitions of 1-4 numbers followed by one to six repetitions of a . followed by one to 6 alphanumeric characters (which is what the regex you used does), you can use your version, just add -E
(assuming your sed
implementation supports it):
$ sed -E 's/shared:core:([0-9]{1,4}(\.[0-9a-z]{1,6}){1,5})/shared:core:1.1.1/' file
shared:core:1.1.1
Or, if you can't use -E
:
$ sed 's/shared:core:\([0-9]\{1,4\}\(\.[0-9a-z]\{1,6\}\)\{1,5\}\)/shared:core:1.1.1/' file
shared:core:1.1.1
Although I fear this might not be 100% portable, not sure.
Finally, remember that sed
doesn't change the input file unless you give it the -i
option. So if you are expecting the original file to change, use sed -i .... file
.
.
followed by one to 6 alphanumeric characters". But this doesn't make much sense with the input example you have given.diff
ing it with a reference version).sed 's/0\.0\.2/1.1.1/
? If so, why do you have such a complicated regex? Are there any more lines in the file that you need to avoid?