3

I am using some sub(), gsub() functions to replace content stored in a variable.

Say for example:

$ awk 'BEGIN {str="hello bee"; patt="llo"; gsub(patt,"XX",str); print str}'
heXX bee

This replaces in the string contained in str all occurrences of llo with XX. Nice.

Now say that I want to use a more complex regular expression that uses both a variable and a pattern. For example, "he" + variable patt. How can I do it?

If I try with gsub(/he/patt, ...) it doesn't work:

awk 'BEGIN {str="hello bee"; patt="llo"; gsub(/he/patt,"XX",str); print str}'

Returns

hello bee

Instead of

XX bee
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Added the [gawk] tag since it is the one I am using. Any solution using another distribution is also more than welcome : ) – fedorqui Oct 01 '15 at 15:06

3 Answers3

4

That's when you need string concatenation and so have to use string delimiters instead of regexp delimiters for the regexp:

$ awk 'BEGIN {str="hello bee"; patt="llo"; gsub("he"patt,"XX",str); print str}'
XX bee

Just be aware that you'll then need to double up any escape chars since awk has to convert the string to a regexp first and so that will use up one of the escape chars.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • Nice! So for example I need to say `gsub("he"patt"\\s","XX",str)`, that is, use `\\s` instead of `\s` to match the space. – fedorqui Oct 01 '15 at 15:14
  • 2
    Correct. If you use a single escape, `\s`, then gawk will give you a warning about the `\s` being treated as a literal `s` and then do so but some other awks won't warn you and IIRC some will interpret it as if you wrote `\\s` while others will treat it as `s`. – Ed Morton Oct 01 '15 at 15:19
3

You can do:

awk 'BEGIN {str="hello bee"; patt="llo"; gsub("he" patt,"XX",str); print str}'
XX bee

/he/patt doesn't concat the string together but "he" patt does.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Oh, nice! So there is no way to concat a string using `/he/`? – fedorqui Oct 01 '15 at 15:09
  • 3
    @fedorui `/he/` is not a string, its a regexp. There is no such thing as regexp concatenation, only string concatenation and conversion of strings to regexps when strings are used in a regexp context. – Ed Morton Oct 01 '15 at 15:10
0

this will work

awk 'BEGIN {str="hello bee"; patt="llo"; gsub(patt,"XX",str); print str}'
heXX bee

essentially you have to define the full pattern as a variable, not part of it.

karakfa
  • 66,216
  • 7
  • 41
  • 56
  • 1
    Yep! This is a way, and thanks for it. Note, though, that I am wondering if there is a way to do such concatenation. – fedorqui Oct 01 '15 at 15:12