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
1
  • Added the [gawk] tag since it is the one I am using. Any solution using another distribution is also more than welcome : ) Commented Oct 1, 2015 at 15:06

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
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.

2 Comments

Oh, nice! So there is no way to concat a string using /he/?
@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.
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.

1 Comment

Yep! This is a way, and thanks for it. Note, though, that I am wondering if there is a way to do such concatenation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.