2

I am trying to replace lines that contain the pattern "s#_decoded_block[" with "s#_decoded_block_". The command I am using to do that is

%s/s\d\+_decoded_block\[/s\d\+_decoded_block_/g

I expect to get "s#_decoded_block_" as substitutions but instead I am getting "sd+_decoded_block_". Please let me know what I am doing wrong.

2 Answers2

2

You seem to expect a decimal number for "#" in the question. You can do this using a backreference:

%s/s\(\d\+\)_decoded_block\[/s\1_decoded_block_/g

where \(\d\+\) matches one or more decimal digits and is replaced using the marker \1.

You could improve the backreference by moving more of the text inside it:

%s/s\(\d\+_decoded_block\)\[/s\1_/g

because \1 will be replaced by whatever was matched in the group.

Thomas Dickey
  • 76,765
1

Not sure if it perfectly fits your use case, but it looks like you may as well just use:

:g/s\d\+_decoded_block\[/ s/\[/_/

...unless there are potentially other [ characters in the matching lines preceding the one you want to change.

Simpler is better, if it fits your requirements.

Wildcard
  • 36,499