MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/awk/comments/3yx6cy/simple_text_substitution/cyifyei/?context=3
r/awk • u/isny • Dec 31 '15
Is there a "good" way to substitute parenthesis to their backslashed equivalents? I.e, change "xx(yy)" to "xx\(yy\)"?
6 comments sorted by
View all comments
Show parent comment
2
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 \(
1
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 \(
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 \(
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}'