0

I am working on the following code in an awk script and I need the output to be redirected to another file within the same script.


BEGIN { FS=OFS="," }
NR==1 {print; next}
{ $9 = sprintf("%0.2f", $9) }
{ a[$0]++ }


BEGIN  { FS=OFS="," }
{ gsub(/\r/,"") }
FNR==1 { $10="Survival Percentage"  }
FNR > 1 && ($5+0==$5 && $6+0==$6 && $3+0==$3){
  $10=sprintf("%0.2f",(($5-$6)/$3)*100)
}1

END {
  if (i>0){
    for  (i in a){
        print "i" > nj.csv
}}}

This is my code and just by executing it I get an error pointing to the point between nj and csv (nj.csv). Any idea to solve it?

joanis
  • 10,635
  • 14
  • 30
  • 40
codder
  • 3
  • 2
  • `if (i>0)`: Where do you assign a value to `i`? – Cyrus May 22 '22 at 19:58
  • I assume you are working on the same homework as he is [here](https://stackoverflow.com/q/72325697/3776858). – Cyrus May 22 '22 at 20:18
  • `print "i" > nj.csv`: This writes the letter `i` to the file stored in the variable `nj.csv`. However, variables must not contain a period in their name. – Cyrus May 22 '22 at 20:29

1 Answers1

0

gsub(/\r/,"") is almost always the wrong thing to do, you probably meant sub(/\r$/,""), and print "i" > nj.csv should be print i > "nj.csv" but idk why you have 2 identical BEGIN sections or what the overall purpose of the script is as it doesn't seem to make any sense.

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