-1

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.

mahmood
  • 23,197
  • 49
  • 147
  • 242
  • Re: "regex substitution is still not solved" -- you haven't told us what your expected output is, so how do we know how the actual output you showed differs from what you intend? Which is to say -- the part that isn't duplicative is presently not clearly communicated enough to be answerable. – Charles Duffy Sep 18 '22 at 17:52
  • Please see the updated post. – mahmood Sep 18 '22 at 17:54
  • That update makes it look to me like fixing the quoting would suffice. What's your new actual output once you apply the fix from the duplicate? – Charles Duffy Sep 18 '22 at 17:54
  • `'s/.buffer_cap_tile=./.buffer_cap_tile=$b/g'` is still single quotes. The duplicate tells you not to use single quotes. That is to say, your code should be `"s/.buffer_cap_tile=./.buffer_cap_tile=$b/g"` or at minimum `'s/.buffer_cap_tile=./.buffer_cap_tile='"$b"'/g'` (switching to double quotes for the part of the string where the substitution is desired). – Charles Duffy Sep 18 '22 at 17:56
  • @CharlesDuffy: Even with `"s/.buffer_cap_tile=./.buffer_cap_tile=$b/g"`, the results is `Rx.buffer_cap_tile=200000000` and not `Rx_buffer_cap_tile=20000`. – mahmood Sep 18 '22 at 18:16
  • 1
    Well, yes, you're only matching and replacing exactly one character (`.` is a single-character wildcard), so that's normal and expected for your regex. If you want to match multiple characters, you should be replacing `.` with `.*`, or maybe `[[:digit:]]*` or so forth. – Charles Duffy Sep 18 '22 at 18:48
  • BTW, if you mean the `.` in `.buffer_cap_tile` to only match the specific character `.`, consider writing it as `[.]` so it's a single-entry character class. – Charles Duffy Sep 18 '22 at 18:51

1 Answers1

-1

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?

brunson
  • 749
  • 4
  • 13
  • 1
    (not sure why this is downvoted -- it's accurately identifying the most immediate problem, though a question caused by not understanding the difference between single and double quotes should be closed as duplicate of existing Q&A we have on the topic) – Charles Duffy Sep 18 '22 at 17:50
  • 1
    (...if the replacement values could be arbitrary there's more work to safely escape them so sed treats them as literal, but the naive approach works well enough when the replacement values are guaranteed to be numeric). – Charles Duffy Sep 18 '22 at 17:51