Skip to main content
4 of 4
deleted 24 characters in body
Rui F Ribeiro
  • 58k
  • 28
  • 156
  • 238

awk - gensub function

I need to insert contents of variable to parameter function gensub in awk, specifically instead of regular expression's parameter.

I have:

gawk '
 BEGIN {
   a = "abc15d56ef";
   b = gensub(/.*([0-9][0-9])d([0-9][0-9]).*/, "\\2 \\1", "g", a);
   print b;
 }'

output

output:56 15

I need it to be in this form, but it doesn't work:

gawk '
 BEGIN {
   a = "abc15d56ef";
   c="/.*([0-9][0-9])d([0-9][0-9]).*/";
   b = gensub(c, "\\2 \\1", "g", a);
   print b;
 }'

output

output:56 15
Mike
  • 31
  • 1
  • 1
  • 3