0

I have a script with two codes to pull the sum of data of a given file and print to another file and a second pulls the total number of missing data points to another file. Finally the two output files are placed together in to a third file. All three codes work when run alone. Due to the large number of files needing to have this done I was attempting to automate in a single script file. Running bash shell. The Colavg.sh file has been execute permissions. When I attempt to run this as a single script I get the following error:

awk: cmd. line:1: ./Colavg.sh
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: ./Colavg.sh
awk: cmd. line:1:   ^ unterminated regexp
#!/bin/awk

awk -F ',' 'NF==4{count++} END {print "missing: "count}' County029-CO-0023-T0000JD.out > avg.out;
awk -F ',' '$4==-999{c999++}; END{print "missing: "c999}' County029-CO-0023-T0000JD.out > avg1.out;
cat avg1.out avg.out >> County029-CO-0023-T0000JD-avg.out
1
  • 5
    What you have there is a shell script that uses awk, not an awk script - change the shebang to an appropriate shell interpreter such as #!/bin/sh or #!/bin/bash Commented Mar 22, 2021 at 13:20

1 Answer 1

2

The issue is your script's #!-line. It should read #!/bin/sh as it's a shell script, not an awk script.

What's really happening here is that the shell, with your erroneous #!-line, would execute

/bin/awk ./Colavg.sh

This means using the text string ./Colavg.sh as the awk program. As an awk program, this does not make sense, and your awk interpreter identifies two separate errors before deciding to bail out.


The script could be simplified:

#!/bin/sh

name=County029-CO-0023-T0000JD

awk -F , '
    $4 == -999 { c999++  }
    NF == 4    { count++ } 
    END { printf "missing: %d\n", c999  }
    END { printf "missing: %d\n", count }' "$name.out" >>"$name-avg.out"

Change >> at the end to > if you want the output file to be truncated (emptied) before the results are written to it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .