I trying to backreference using the sed command

Posted by Paul on Stack Overflow See other posts from Stack Overflow or by Paul
Published on 2010-05-21T13:46:20Z Indexed on 2010/05/21 13:50 UTC
Read the original article Hit count: 281

Filed under:

I am relative new to shell scripting and sed. I need to substitute a pattern, globably, but I also need to remember (or save) part of the pattern and use it later in the same substitute command. The saved pattern will be varible, so I need to use a wild card. For example,

input message=trt:GetAudioSourcesRequest/>

and I want to end up with something like

input message=trt:GetAudioSourcesRequest PAUL/GetAudioSourcesRequest/>

but the function string "GetAudioSourcesRequest" will change (in length also) throughtout the file, so I need a wild card, e.g.

sed -i "s/input message=trt:<wild card in here>/>/input message=trt:<print wild card> PAUL/<print wild card>/>

I have managed to get the following command to nearly do what I want but it is too rigid. It only stores a 4 syllable pattern so if I have a function name such as GetProfileRequest, this doesn't work

echo "input message=\"trt:GetAudioSourcesRequest\"/>" | sed 's/input message=\"trt:\([A-Z][a-z]*\)\([A-Z][a-z]*\)\([A-Z][a-z]*\)\([A-Z][a-z]*\).*/input message=\"trt:\1\2\3\4\ PAUL\/\1\2\3\4"\/\>/g'

This outputs

input message="trt:GetAudioSourcesRequest PAUL/GetAudioSourcesRequest"/>

Which is ok but when I use GetProfileRequest this doesn't.

I have come accross \W and [^[:alnum:]] or [[:alnum:]] but I don't how to use them

Thanks in advance.

© Stack Overflow or respective owner

Related posts about shell-scripting