Using sed command with xargs and bash variables

Viewed 42

I would like to use sed with regex combination and pass variable to xargs command in this way:

$ cat test.txt
Rx_buffer_cap_tile=10000
Tx_buffer_cap_tile=10000
1132:1132:500:1132:4000
$ b=20000
$ d=8000
$ find . -type f -name test.txt -print0 | xargs -0 sed -i 's/.buffer_cap_tile=./.buffer_cap_tile=$b/g'
$ find . -type f -name test.txt -print0 | xargs -0 sed -i 's/500:1132:.*/500:1132:$d/g'

But the output is

Rx.buffer_cap_tile=$b0000
Tx.buffer_cap_tile=$b0000
1132:1132:500:1132:$d

The expected output is

Rx.buffer_cap_tile=10000
Tx.buffer_cap_tile=10000
1132:1132:500:1132:8000

How can I fix that?

P.S: If there are others ways like awk or perl, they are all accepted.


UPDATE:

The suggestion for using " instead of ' works for variable expansion. However, considering the original

Rx_buffer_cap_tile=10000
Tx_buffer_cap_tile=10000

and using "s/.buffer_cap_tile=./.buffer_cap_tile=$b/g", the result is

Rx.buffer_cap_tile=200000000
Tx.buffer_cap_tile=200000000

Which is not correct.

1 Answers

I believe the single quotes, '...',around your sed command inhibits bash from interpolating your shell variables. Try double quotes: "..."

This doesn't have anything to do with sed, it is a feature of the bash shell.

Also, I think there's a typo in your first sed statement. Did you miss the b after the dollar sign?

Related