gensub()
expects a string as second argument. You are trying to concatenate the /
and ,
around an expression (\\1+3)
which you assume will be evaluated by the function. It will not. It is evaluated before calling the function. You use \1
to refer to the matching capture group ()
in the regexp, but you can only use it in a string, not in an expression.
So at best you could use as second argument "/\\1+3,"
, but you would then get the result ...Backslash/49+3,Black
. You cannot evaluate the 49+3 part in this way.
If you want to do arithmetic on the match, you must first extract the string, do the arithmetic, then place it back in the string. For example,
awk '{ n = split($0, d, /\/([0-9]+),/, s)
print d[1] "/"(substr(s[1],2)+3)"," d[2] }'
This uses gnu awk's split()
function with a regexp to split the line into 3 parts: the part before the match in d[1]
, the part after the match in d[2]
, and the matched string "/49,"
in s[1]. You should really check n
is 2 to ensure you got exactly one match.
You can then extract the number from the matched string by simply skipping over the initial "/"
, do the arithmetic, and concatenate all the parts together again.
If the pattern may appear several times in one line of your data, a better solution is to use match()
to find only the last occurence and cut up the line using substr()
:
awk '{ match($0, /.*\/([0-9]+),/, m)
a = m[1,"start"]
b = m[1,"length"]
if(a)print substr($0,1,a-1) substr($0,a,b)+3 substr($0,a+b)
else print }'
Here the pattern has .*
added at the front to match only the last occurence.
a
is set to the character position of the start of the capture group ()
in the regexp, and b
to its length, so substr($0,a,b)
is just the number. The final line is reassembled from the two other parts of the original data.