screen output redirection to file does not work properly with awk
by bsmile from LinuxQuestions.org on (#6EKCB)
The 3rd column of a file is sorted in chunks and I would like to use awk to filter out a range of lines the sorted chunk into an individual file using the following awk script
Code:(NR > 89 && NR < 142){ # specifically filter lines between 89 and 142
if($3>e-0.001){ # if 3rd column keeps ascending, then output
print $2 " " $3; fflush()
}
else{ # otherwise,
if(NR-89>20){
exit # quit if the right chunk is output
}
else{ # delete existing file as output might from previous chunk
system("rm outfile") # involve shell command to remove the file
system("touch outfile") # desperately want to revive the file
}
};
e=$3
}Code:awk -f script_awk datafile
If I don't use redirect, but just output on the screen, it can correctly do its job as shown below,
...
100 0.1424964
101 0.1554909
102 0.1569432
rm: can't remove 'outfile': No such file or directory
104 0.0408131
105 0.0410339
106 0.0424384
107 0.0489975
108 0.0495339
...
Code:awk -f script_awk datafile > outfilebut if I use redirect, then nothing is saved in outfile. Same result if I use "tee".
Code:(NR > 89 && NR < 142){ # specifically filter lines between 89 and 142
if($3>e-0.001){ # if 3rd column keeps ascending, then output
print $2 " " $3; fflush()
}
else{ # otherwise,
if(NR-89>20){
exit # quit if the right chunk is output
}
else{ # delete existing file as output might from previous chunk
system("rm outfile") # involve shell command to remove the file
system("touch outfile") # desperately want to revive the file
}
};
e=$3
}Code:awk -f script_awk datafile
If I don't use redirect, but just output on the screen, it can correctly do its job as shown below,
...
100 0.1424964
101 0.1554909
102 0.1569432
rm: can't remove 'outfile': No such file or directory
104 0.0408131
105 0.0410339
106 0.0424384
107 0.0489975
108 0.0495339
...
Code:awk -f script_awk datafile > outfilebut if I use redirect, then nothing is saved in outfile. Same result if I use "tee".