Sed question to substitute data in \2

Hi All,
Here is what I'm trying to do with sed:

Input File:

somechangeVariable1=Something I would like to change
somechangeVariable2=Something else I would like to change
...

Output File:

somechangeVariable1=Something I would like to different
somechangeVariable2=Something else I would like to different
...

So I tried this unsuccessfully in sed:

/\(.*\)=\(.*\)/{
	/\2/ s/change/different/
}

Error:

line 2: Invalid back reference

Question:
How do I run the substitute on \2 alone?
I realize that I can use other tools but would like to solve this with sed as more of a theoretical exercise at this point.

Thanks in advance for any help provided.
Peace!

$
$ cat f2
somechangeVariable1=Something I would like to change
somechangeVariable2=Something else I would like to change
$
$ sed 's/^\(.*=.*\) \(.*\)$/\1 different/' f2
somechangeVariable1=Something I would like to different
somechangeVariable2=Something else I would like to different
$
$

tyler_durden

Clever but not quite what I wanted;
Here is the rephrased question:
Hi All,
Here is what I'm trying to do with sed:

Input File:

somechangeVariable1=Something I would like to change something
somechangeVariable2=Something else I would like to change else
...

Output File:

somechangeVariable1=Something I would like to different something
somechangeVariable2=Something else I would like to different else
...

So I tried this unsuccessfully in sed:

/\(.*\)=\(.*\)/{
	/\2/ s/change/different/
}

Error:

line 2: Invalid back reference

Question:
How do I run the substitute on \2 alone?
I realize that I can use other tools but would like to solve this with sed as more of a theoretical exercise at this point.

Thanks in advance for any help provided.
Peace!

sed 's/\(.*\)change\(.*\)$/\1different\2/' file
OR
sed 's/\(.*\)=\(.*\)change\(.*\)/\1=\2different\3/' file
sed 'h; s/=.*/=/; x; s/^[^=]*=//; s/change/different/g; H; g; s/\n//'

Sample run:

$ cat data
somechangeVariable1=Something I would like to change something
somechangeVariable2=Something else I would like to change else
somechangeVariable3=change change change and still more change
leavealone=nothing to see here
$ sed 'h; s/=.*/=/; x; s/^[^=]*=//; s/change/different/g; H; g; s/\n//' data
somechangeVariable1=Something I would like to different something
somechangeVariable2=Something else I would like to different else
somechangeVariable3=different different different and still more different
leavealone=nothing to see here

Explanation:

  1. h: Copy the original, pristine, unaltered line to the hold space, for future use.
  2. s/=.*/=/: From the current line, remove everything that follows the equals sign inclusive, replacing it with just the equals sign. What remains is the variable name (with the trailing equals sign), in which we are not interested in substituting anything.
  3. x: Store the variable name in the hold space by swapping the pattern and hold spaces. The original version of the line is now back in the pattern space.
  4. s/^[^=]*=//: Remove all leading characters that are not an equals sign and the first equals sign, leaving nothing but the text of interest to the primary substitution operation.
  5. s/change/different/g: Substitute each instance of "change" with "different" in the relevant text.
  6. H: Append the resulting string to the variable name which we've kept tucked away in the hold space.
  7. g: Copy the hold space to the pattern space.
  8. s/\n//: Remove the newline that was inserted between the variable name and variable value by step 6's H command.
2 Likes

Thanks Alister. Precisely what I was looking for.
Peace be with you.

Thank you for adding the explanation, It was quite interesting :slight_smile: