Modify capture group

Hi, is it possible to modify captured group in substitution, e.g. to output \1 in 2 columns, first one unchanged and 2nd one change to upper case + replace - with underscore. I only did the first part:

s/\(a.*\)/\1 \L\1/g

Can i do the replacement in the same command?

Thanks a lot.

The & character references the unchanged text that matches the pattern.

so If I get what you want:

s/\(a.*\)/\1 \L\1/g -> s/\(a.*\)/\1 \L\1/_&/g

But I'm not clear on what you mean by the second one versus the first one. So the example could be wrong.

Please give an example of sample input and expected output. Thanks.

I'm afraid that can't be done, not even with a second substituition, as the start of the upper case replacement string can't be retrieved reliably and consistently.

That is what I wondered, but I though he wanted the original with a prepended '_'
character.
e.g.:

a.foo -> A.FOO _a.foo

Since we have no answer we cannot know.

Thanks to all of you. I intend to do something like:

abc-efg -> abc-efg ABC_EFG

Just wonder if this can be done in the same sed script.

Thanks again.

This cannot be generalised; it won't handle multiple dashes, it will fail on input with different structure, it's ugly:

echo abc-efg | sed 's/\([a-z]\+\)\(-*\)\([a-z]\+\)/\1\2\3 \U\1_\3/g'
abc-efg ABC_EFG

There will be multiple dashes at various locations... Seems i've to do one more pipe.

Thanks a lot.
Rgds

Please give us an example of the input you have and the output you are trying to produce. I don't see why another pipe would be needed, but I don't think I understand what you're trying to do.