Q:

Grep, Sed... Awk to modify lines in a file

I've searched for this but I didn't find exactly what I'm looking for.
What I want to do is replace in the same file all the lines that contains a jar file with "PATTERN" text in the name so that I can add in those lines a new property with the sourcecode for those jar files. The sourcecode for those jars will be always in same relative folder (i.e. path1/lib/a.jar -> path1/src/java)
An example of this. This is my original file:
<classpathentry path="/sde/ATG/ATG10.1.2/Momentum/Search/I18N/lib/MOM-Search-I18N-0.23-ECI.jar" />
<classpathentry path="/sde/ATG/ATG10.1.2/DAS/lib/axis-1.4.jar" />
<classpathentry path="/sde/ATG/ATG10.1.2/REST/lib/org.json.jar" />
<classpathentry path="/sde/ATG/ATG10.1.2/Momentum/StoreFront/lib/MOM-Search-I18N-Index-0.23-ECI.jar" />

And I want to get this:
<classpathentry path="/sde/ATG/ATG10.1.2/Momentum/Search/I18N/lib/MOM-Search-I18N-0.23-ECI.jar" sourcepath="/sde/ATG/ATG10.1.2/Momentum/Search/I18N/src/main/java" />
<classpathentry path="/sde/ATG/ATG10.1.2/DAS/lib/axis-1.4.jar" />
<classpathentry path="/sde/ATG/ATG10.1.2/REST/lib/org.json.jar" />
<classpathentry path="/sde/ATG/ATG10.1.2/Momentum/StoreFront/lib/MOM-Search-I18N-Index-0.23-ECI.jar" sourcepath="/sde/ATG/ATG10.1.2/Momentum/StoreFront/src/main/java"/>

I need to add a sourcepath attribute to the lines with my pattern and that sourcepath value should take the root of the path value.
As 
grep -o 'path="[/-.0-9A-Za-z]*/lib/MOM[-.0-9A-Za-z]*.jar"' test.txt 

gives me the lines that contain lines with the jars I'm looking for, I thought that this would solve my problem:
cat test.txt | sed -r 's|path="[/-.0-9A-Za-z]*/lib/MOM[-.0-9A-Za-z]*.jar"|\1 sourcepath="\2/main/src/main/java"/>|'

But gives me this error: sed: -e expression #1, char 91: invalid reference \2 on `s' command's RHS
Any idea?
Thanks guys!

A:

You could say:
sed 's| \(path=.*\)\(/lib\)\(/MOM[^ ]*\)| \1\2\3 source\1/src/main/java"|' inputfile

For your sample input, it'd produce:
<classpathentry path="/sde/ATG/ATG10.1.2/Momentum/Search/I18N/lib/MOM-Search-I18N-0.23-ECI.jar" sourcepath="/sde/ATG/ATG10.1.2/Momentum/Search/I18N/src/main/java" />
<classpathentry path="/sde/ATG/ATG10.1.2/DAS/lib/axis-1.4.jar" />
<classpathentry path="/sde/ATG/ATG10.1.2/REST/lib/org.json.jar" />
<classpathentry path="/sde/ATG/ATG10.1.2/Momentum/StoreFront/lib/MOM-Search-I18N-Index-0.23-ECI.jar" sourcepath="/sde/ATG/ATG10.1.2/Momentum/StoreFront/src/main/java" />

