I want to use awk to edit a column of a large file inplace
. If, for any reason, the process break/stops, I don't want to lose the work already done. I've tried to add fflush
but seems it does not wort with inplace
.
In order to simulate the desired result, here is a test file with 3 columns. The last column is all zeros.
paste -d '\t' <(seq 1 10) <(seq 11 20) |
awk 'BEGIN {FS="\t"; OFS=FS} {$(NF+1)=0; print}' > testfile
Then I want to replace the values in last column. In this simple example, I'm just replacing them by the sum of the first and second columns. I'm adding a system sleep so it might be possible to abort the script in the middle in see the result.
awk -i inplace 'BEGIN {FS="\t"; OFS=FS} $3==0{$3=$1+$2; print; fflush(); system("sleep 1")}' testfile
If you run the script and abort it (ctrl+z) before it ends, the test file is unchanged.
Is it possible to achieve the desired result (get the partial result when the script breaks or stops)? How should I do it?