how do you avoid the overlap of the first argument that is replaced by sed?

Assuming that the Grp-N entries do not appear at the end of a line, then you can use:

sed -e 's/Grp-0\([^0-9]\)/Grp-19\1/; s/Grp-1\([^0-9]\)/Grp-20\1/'

This looks for Grp-0 followed by a non-digit and maps it to Grp-19 followed by the non-digit. Similarly for Grp-1 followed by a non-digit, and so on.

Another option is to start a new cycle after a substitution occurs:

sed -e 's/Grp-0/Grp-19/; t; s/Grp-1/Grp-20/; t …'

The t command with no named label jumps to the end of the script if a s/// command has done a successful substitution since the last input line was read and since the last t command.

With things sequenced in increasing order, you can still run into problems if you have enough of these mappings. If you sequence the tests in reverse order, you don’t run into problems — map Grp-19 before you map Grp-0 (with the t to skip).

However, you should aim to avoid writing out a systematic mapping longhand. Tools like awk or Perl can do arithmetic, so when they find Grp-NN, they can substitute NN+19 for the original value.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top