Is it possible to manipulate the ampersand in sed? I want to add +1 to all numbers in a file. Something like this:
sed -i "s/[0-9]\{1,2\}/$(expr & + 1)/g" filename
EDIT: Today I created a loop using grep and sed that does the job needed. But the question remains open if anyone knows of a way of manipulating the ampersand, since this is not the first time I wanted to run commands on the replacement string, and couldn't.
sed will need to thunk out to some shell command (with '!') on each line to do that.
Here you think you are calling sed which then calls back to the shell to evaluate $(expr & + 1)
for each line, but actually it isn't. $(expr & + 1)
will just get statically evaluated (once) by the outer shell, and cause an error, since '&' is not at that point a number.
To actually do this, either:
hardcode all ten cases of last digit 0..9, as per this example in sed documentation
Use a sed-command which starts with '1,$!' to invoke the shell on each line, and perform the increment there, with expr, awk, perl or whatever.
FOOTNOTE: I never knew about the /e modifier, which php-coder shows.