Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulate ampersand in sed

Tags:

sed

ampersand

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.

like image 495
Teresa e Junior Avatar asked Jan 23 '26 09:01

Teresa e Junior


1 Answers

You may use e modifier to achieve this:

$ cat test.txt
1
2
$ sed 's/^[0-9]\{1,2\}$/expr & + 1/e' test.txt
2
3

In this case you should construct command in replacement part which will be executed and result will be used for replacement.

like image 127
Slava Semushin Avatar answered Jan 25 '26 06:01

Slava Semushin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!