[PLUG] gawk: escaping parentheses in pattern string

Tomas Kuchta tomas.kuchta.lists at gmail.com
Sat Mar 28 23:01:05 UTC 2020


You did not give example of the text you are parsing. So, I assume that you
want to replace B(W) literal in column 2 with number 11.

Try this:
awk '$2 == "B(W)" { $2=11; print $0 }'
Or as regexp:
awk '$2 ~ /B(W)/ { $2=11; print $0 }'

Tomas

On Sat, Mar 28, 2020, 14:33 Rich Shepard <rshepard at appl-ecosys.com> wrote:

> The second field in a file is a string (but not demarked as such with
> quotes) and I want to replace it with a number. The gawk pattern match for
> 40 of these strings work, but when $2 == B(W) gawk doesn't like it.
>
> /$2 ~ B(W)/ { $2=11; print $0 }
>
> I tried escaping the parentheses with backslashes \( and \) and quoting the
> string "B(W)" but both result in a syntax error. I've not found the proper
> way to replace B(W) with 11 and need a clue.
>
> Rich
> _______________________________________________
> PLUG mailing list
> PLUG at pdxlinux.org
> http://lists.pdxlinux.org/mailman/listinfo/plug
>



More information about the PLUG mailing list