error trying to redirect output of awk script to a new file

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.

What I try to do in the first block is to round the decimals of the numbers in column 9, and in the second I want to create a new column from calculating a percentage. The output is correct but at the end I want to send all the lines to a file.

BEGIN { FS=OFS="," }
NR==1 {print; next}
{ $8 = sprintf("%0.4f", $8) }
{ a[$0]++ }
BEGIN { FS=OFS="," }
{ gsub(/\r/,"") }
FNR==1 { $10="Column" }
FNR > 1 && ($12+0==$12 && $16+0==$16 && $13+0==$13){ $10=sprintf("%0.4f",(($15-$16)/$13)*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?

10

1 Answer

You appear to be stream-processing records, and redirecting the result to a single file with fixed name. So there's no need to store anything (a[$0]++) nor to loop over anything at the END, nor to do the redirection inside awk (you can simply shell-redirect the output of the whole command).

My best guess of what you are trying to do is:

awk -v RS='\r?\n' -F, ' BEGIN{OFS = FS} NR == 1 {$10 = "Survival Percentage"} NR > 1 { $9 = sprintf("%0.2f",$9) $10 = ($5+0==$5 && $6+0==$6 && $3+0==$3) ? sprintf("%0.2f",(($5-$6)/$3)*100) : "N/A" } {print}
' input.csv > nj.csv

I removed the gsub(/\r/,"") in favour of setting the input record separator to handle an optional CR. This is really equivalent to sub(/\r$/,"") - I'm assuming that you don't really need to handle CR characters except at the EOL.


To make it into a script:

BEGIN{RS = "\r?\n"; FS = ","; OFS = FS} NR == 1 {$10 = "Survival Percentage"} NR > 1 { $9 = sprintf("%0.2f",$9) $10 = ($5+0==$5 && $6+0==$6 && $3+0==$3) ? sprintf("%0.2f",(($5-$6)/$3)*100) : "N/A" } {print}

and then execute with redirection handled by your shell as

awk -f awk_script.awk dataset.csv > nj.csv

or make it an executable awk script:

#!/usr/bin/awk -f
BEGIN{RS = "\r?\n"; FS = ","; OFS = FS} NR == 1 {$10 = "Survival Percentage"} NR > 1 { $9 = sprintf("%0.2f",$9) $10 = ($5+0==$5 && $6+0==$6 && $3+0==$3) ? sprintf("%0.2f",(($5-$6)/$3)*100) : "N/A" } {print}

make it executable, then run as

./awk_script.awk dataset.csv > nj.csv

If you insist on placing the output redirection inside awk then it needs to be written as {print > "nj.csv"}.

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like