gsub(pattern, replacement, target)
: allows a variable to be used for pattern, but does not let me do regular expression.
gsub(/pattern/, replacement, target)
: lets me do regular expression, but I cannot use a variable for the pattern
.
Is there a way to get both variable pattern and regex to work in gsub
? I'd like to stick with awk, no sed or shell.
If you mean something like ruby:
/foo#{pat}bar/
that's not possible in awk (that way). But you can build the pattern when calling gsub.
pat = "[a-z]+"
gsub("foo" pat "bar", rep, target)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With