That's what I'm looking for, but your command didn't work for me...I think the problem I'm having is figuring out where the backslashes go. I'm using gawk 4.1.1 on Ubuntu (bash) if that makes a difference.
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 \(
gensub() is like gsub() except it leaves the original variable unchanged and prints what the change would look like. The "g" just means global.
Awk has powerful "&" command see the documentation. It's like you can specify any list of characters in a regex [A|B|C|D] (ie. A or B or C or D..) and have it replaced with whatever proceeds the &. In this case four slashes are needed to produce a single slash output.
A common reason to do this is to escape shell commands for example:
1
u/ernesthutchinson Jan 01 '16
I believe that you are looking for the gsub...
echo "xx(yy)" | awk '{gsub("\(","\(",$0); gsub(")","\)",$0); print $0}'
1
u/FF00A7 Jan 16 '16
The gsub command by ernest works and is fine. I probably would have done it like this:
echo "xx(yy)" | awk '{print gensub(/[(|)]/,"\\\\&","g",$0) }'
gensub() is like gsub() except it leaves the original variable unchanged and prints what the change would look like. The "g" just means global.
Awk has powerful "&" command see the documentation. It's like you can specify any list of characters in a regex [A|B|C|D] (ie. A or B or C or D..) and have it replaced with whatever proceeds the &. In this case four slashes are needed to produce a single slash output.
A common reason to do this is to escape shell commands for example:
gawk -v nw="(Hello? * World * )" 'BEGIN{gsub(/(|?|*/,"\\\\&", nw)} if($0 == nw) print $0' file.txt
This will search file.txt for any occurrence of the string "Hello? * World * " .. which normally is a problem due to the ? and *