Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing regex groups with sed

Tags:

regex

bash

sed

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=''".

like image 236
alexandernst Avatar asked Feb 05 '14 15:02

alexandernst


People also ask

How do you use groups in SED?

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.

Does SED support lookahead?

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.

What is a capturing group regex?

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" .


1 Answers

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"
like image 153
Mad Physicist Avatar answered Sep 20 '22 05:09

Mad Physicist