1

Running a build script for a 3rd party library and getting this awk error.

In rhel8 gawk 4.2.1 the follow executes without error:

version_num=stable/3.0
printf "3.0.4" | awk "/^${version_num/\//\\\/}/{print;}"

but in rhel7 gawk 4.0.2 you get:

awk: cmd. line:1: /^stable\\/3.0/{print;}
awk: cmd. line:1:                ^ syntax error

I think maybe the / before {print;} is wrong, but it's only a guess and i'm not sure why it works on rhel8...the release notes for the intervening gawk versions didn't show anything that looked relevant.

user109078
  • 906
  • 7
  • 19
  • 5
    This is incorrect use of `awk`. Use `-v` to pass variables – Gilles Quénot Apr 25 '23 at 22:56
  • 2
    check your `bash` versions on both systems. I bet you find an older version on one or the other. But note that `/^stable\/whatever/` won't match just `3.0.4`. The anchored match of `/^stable/ means the string must contain `stable` as the first chars. Even without the `^` char, it won''t match. But pluse-uno for providing a perfect [mcve]! Good luck. – shellter Apr 25 '23 at 23:35

2 Answers2

3

It's not clear from your question what you're really trying to do but use this as a starting point:

version_num='stable/3.0'
printf '3.0.4\n' | awk -v ver="$version_num" 'index($0,ver)'

See How do I use shell variables in an awk script? and How do I find the text that matches a pattern? for more information on what that does and why what you currently have is wrong in a few areas.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
1

@user109078 : "I think maybe the / before {print;}" —- your guess is close - it's the other / :::

just change ...

"/^${version_num/\//\\\/}/{print;}"   

… to …

"/^${version_num/\//\\/}/{print;}"   

then it works fine on all my awk variants.

  • nawk version 20200816
    
    program = |/^stable\/3.0/{print;}|
    

  • mawk 1.3.4
    
    version_num=stable/3.0;
    printf "3.0.4" | mawk -Wd  "/^${version_num/\//\\/}/{print;}"
    MAIN
    000 . omain
    001 . match0  0x14800c788 /^stable/3.0/
    

  • gawk 5.2.1
    
      # gawk profile, created Wed Apr 26 06:13:11 2023
    
      # Rule(s)
    
       1  /^stable\/3.0/ {
          print
      }
    

That said, like others said, pass shell variables the proper way if possible

For simpler items, if you don't really need regex, a straight up reference string lookup saves you the need to escape necessary characters

version_num=stable/3.0; 

printf "stable/3.0.4" | 

gawk -p- -e 'index($0, __)' __="$version_num"

stable/3.0.4

    # gawk profile, created Wed Apr 26 06:23:34 2023

    # Rule(s)

     1  index($0, __) { # 1
     1      print
    }
RARE Kpop Manifesto
  • 2,453
  • 3
  • 11