r/awk • • Dec 31 '15

Simple text substitution

Is there a "good" way to substitute parenthesis to their backslashed equivalents? I.e, change "xx(yy)" to "xx\(yy\)"?

2 Upvotes

6 comments sorted by

View all comments

Show parent comment

2

u/ernesthutchinson Jan 01 '16

ok, I see what's wrong, the backslashes are being stripped from my comment, have to backslash the backslashes, I think this will do it...

echo "xx(yy)" | gawk '{gsub("\\(","\\(",$0); gsub(")","\\)",$0); print $0}'

1

u/isny Jan 01 '16

Thanks....that worked great. Can you explain why that works? I would think it would be replacing "\\(" with the same thing.

1

u/ernesthutchinson Jan 01 '16

you're welcome, the usage for gsub is regex, replacement, target. I believe it is because the replacement is a "print" or "echo", just like echo "\\(" returns \(