I'm trying to replace (with sed) a group matched with a regex, but the best I can get out of my tests is a string that replaces the entire string on the right side of the sed separator.
Example:
echo "this is a sample id='42' string" | sed -r "s/id='(.*?)'/\11/g"
Output:
this is a sample 421 string
Desired output:
this is a sample id='1' string
Is this possible? How?
EDIT:
What I'm trying to do is to actually replace just the group matched by the regex, not the entire string on the left side of the sed script. Said with other words: I want to replace just the '42' with '1' without using "id=''".
Grouping can be used in sed like normal regular expression. A group is opened with “\(” and closed with “\)”. Grouping can be used in combination with back-referencing. Back-reference is the re-use of a part of a Regular Expression selected by grouping.
Note that SED doesn't have lookahead and therefore doesn't support the regex feature you were trying to use. It can be done in SED using other features, as others have mentioned.
Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d" "o" and "g" .
In sed
, the entire left regex is relpaced by the entire right regex. If you would like to preserve a portion of the left regex, you must explicitly copy it to the right or make it a capturing group as well:
echo "this is a sample id='42' string" | sed -r "s/id='(.*?)'/\11/g"
will correctly replace all of the match, id='42'
, with 421
since \1
is 42
. If you want to preserve the part that says id=...
, you have to put it in the replacement:
echo "this is a sample id='42' string" | sed -r "s/id='(.*?)'/id='1'/g"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With