The issue is you're using the wrong output redirection operator. You should be using >
not >>
. Awk does not behave the same as shell wrt these 2 operators. man awk for how those operators work in awk and change your script to:
BEGIN { print "Splitting file ";} { print NR; r=int($2/1024); if(r>5){ print $0 > "testFile";} if(r<=5){ print $0 > "testFile2";} } END { print "Done"; }
to get it to work, and then clean it up to:
BEGIN { print "Splitting file " }
{ print NR; print > ("testFile" (int($2/1024)>5?"":"2")) }
END { print "Done" }
You do NOT need to close the files after every write.
In response to @Aryan's comment below, here are the >
and >>
awk vs shell equivalents:
1) awks >
awk:
{ print > "foo" }
shell equivalent:
> foo
while IFS= read -r var
do
printf "%s\n" "$var" >> foo
done
2) awks >>
awk:
{ print >> "foo" }
shell equivalent:
while IFS= read -r var
do
printf "%s\n" "$var" >> foo
done