Reputation: 21
I have this string: '$'nwnwnwnnn
And want to change it to: { bitset<9>(0bnwnwnwnnn), '$'},
I've looked at many similar questions for different shells using their methods but nothing has worked. I'm generally in zsh but I can use bash or another shell.
The general form I've been trying is this:
sed -E -i new s/(\'.\')([nw]+)/{ bitset<9>(0b\2), \1},/g thing.txt
It should work for any character other than $
and any sequence of n
or w
.
I'm generally confused as to what I need to escape here. Some answers on this site said to escape the parenthesis in the first part of the substitution.
Am I using -i
incorrectly?
Upvotes: 2
Views: 34
Reputation: 782130
You need to escape the parentheses to create a capture group if you're using basic regexp, you don't escape them if you're using extended regexp. The -E
option to GNU sed
, and the -r
option to standard sed
, enable extended regexp, so you don't need to escape them.
If you only want to match $
rather than allow any character in the quotes, you need an escaped $
.
You need to put the entire s///
command inside quotes, as it must be a single argument to the sed
command.
When using -i
, it's conventional to put a .
before the suffix. Also, the suffix is put on the saved copy of the original file, not the new file that you're creating with the changes, so new
is a poor suffix.
sed -E -i .bak "s/('\$')([nw]+)/{ bitset<9>(0b\2), \1},/g" thing.txt
Upvotes: 1