0
win7-64
cygcheck 3.1.7
gawk 5.1.0, API: 3.0 (GNU MPFR 4.1.0, GNU MP 6.2.1)

gawk outputs an error for the program invocation line. The program works in debug mode (gawk -D -f create) but not from inside my cygwin shell. Other gawk programs work fine. Please tell me what I am doing wrong?

 > create 2000_RDA.csv
   gawk: cmd. line:1: ./create
   gawk: cmd. line:1: ^ syntax error
   gawk: cmd. line:1: ./create
   gawk: cmd. line:1:   ^ unterminated regexp

The program

 #! /bin/gawk

 BEGIN {
    outputLine[1] = "   const string ";
    outputLine[2] = " [] = { "
    outputLine[4] = "                           , "
    outputLine[5] = "                           }\n"
 }
 BEGINFILE {
    line = 1;
    name = "y"  gensub(/_RDA.csv$/, "", "g", FILENAME);
    printf("%s%s%s", outputLine[1], name, outputLine[2]);
 }
 ENDFILE {
    printf("%s", outputLine[5]);
 }
 {
    if (($0  ~ /^[[:digit:]]/)) {       # begins with a number
       if (line == 1) { printf("\"%s\"\n", $0); line = 0; }
       else           { printf("%s\"%s\"\n", outputLine[4], $0); }
    }
 }


 
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
lostbits
  • 928
  • 1
  • 9
  • 23
  • Did cp create xxx; > xxx and > gawk -f xxx. Same results as before with "xxx" replacing "create".. Did a which/whereis, both unable to find another create. Commented every line - same result – lostbits Mar 13 '21 at 06:19
  • Having extra blanks at the start of each line of the script in your question meant that a copy/paste/execute of your code produced different errors than the one you reported and so hid the actual problem so I removed those extra blanks. As @Shawn mentions the problem is your shebang but it's best not to use a shebang to call awk anyway and instead simply call awk within the script, see https://stackoverflow.com/a/61002754/1745001. – Ed Morton Mar 13 '21 at 13:17
  • @Ed Morton "#!/usr/bin/env bash gawk ' whatever '" does not work with the existing code. The code embedded double quotes are an issue. But "#!/bin/gawk -f" does work, see below.. I will be researching #!/usr/bin/env, didn't know it exists and I am just plain curious. Thanks – lostbits Mar 13 '21 at 17:51
  • Yes, it does work (assuming you put a newline between bash and gawk) - thousands of people use it in thousands of scripts and there are no issues with embedded double quotes (there would be with literal single quotes but there's a common way to address that issue if you had it, which you don't). If you [edit] your question to include a minimal script that demonstrates the issue you had doing that and tell us in what way it "did not work" (the worst possible problem description as it provides nothing to go on) then we can help you debug it. – Ed Morton Mar 13 '21 at 19:41

1 Answers1

2

Your shebang line is wrong. To use a file as an awk script, you have to add the -f option:

#!/bin/gawk -f

Shebang lines work by having the pathname of the script file and the arguments passed to the script appended to the line, so running create 2000_RDA.csv becomes /bin/gawk ./create 2000_RDA.csv, which means gawk treats ./create as the awk code to run. Adding the -f makes it /bin/gawk -f ./create 2000_RDA.csv, which will execute the code in the file (Which is now the argument of -f).

Shawn
  • 47,241
  • 3
  • 26
  • 60