awk - Rename output file, after processing, same as input file

I have one input file ABC.txt and one output DEF.txt. After the ABC is processed and created output, I want to rename ABC.txt to ABC.orig and DEF to ABC.txt. Currently when I am doing this, it does not process the input file as it cannot read and write to the same file. How can I achieve this?

Is it possible if I can put a time interval between the awk script and mv commands??

This would resolve my issue.

Example:

awk ' $1=="01" { print $2 $4}' ABC.txt > DEF.txt
mv ABC.txt ABC.orig
mv DEF.txt ABC.txt

ABC.txt

01  03  03  04

DEF.txt

01  08  AA FA

what exactly is the error and what exactly is your awk script doing?
As it looks, you are not reading and writing at the same time, can you explain further?

Also, you awk example is incorrect, perhaps you mean the following?

awk ' $1=="01" { print $2 $4}' ABC.txt > DEF.txt

Actually it does not show any error but does not process the file.
And you are right, the awk script i types was in incorrect format.
But even the corrected one is not processing the file.
I am trying this script in a unix box.

What happens if you run:

awk ' $1=="01" { print $2 $4}' ABC.txt

Do you get the correct output ?

Otherwise, what is/was the exact error message that you are/were getting?

I have tried the same script but it does not rename the DEF.txt to ABC.txt.
If i put

mv DEF.txt XYZ.txt 

Then it works fine

I see, but what is the answer to my questions?

Yes i got the correct output
But no error message is displayed.
It doesnot work at all.
To my understanding, it is reading and writing to the file in the same time.
Is it possible if I can put a time interval, lets say, 2 seconds between Script and mv command?

Actually this is just an example of the input.
Actual input file has about 10,000 records.
If I can put a time interval I beleieve it would resolve my issue.

So it is just

mv DEF.txt ABC.txt

that is giving you trouble?

In post #1 you mentioned an error. What is the exact error message..

Since i dont see any error message ,

I cant tell if it is coming from thie 2nd or the 1st command but to my understanding, it is coming from mv DEF.txt ABC.txt

I misused the word error. Actually there is no error message.
I am going to update my question.

Looking at your post #1, what keeps you from

mv ABC.txt ABC.orig
awk ' $1=="01" { print $2 $4}' ABC.orig > ABC.txt

?

Is it possible if I can put a time interval between the awk script and mv commands??

This would resolve my issue.

Yes you can put a time interval between the awk script and the mv command. (Look at the man page for sleep .) Doing so will make your shell script run longer, but will not make any difference in the results on any properly working system.

Unless something else is going on that you have not shown us, sleeping for a microsecond or sleeping for an hour will not make any difference at all.

You have shown us that awk is reading a file and nothing is writing to that file until awk finishes. Then, only after awk finishes, you mv a file. No delay added between the awk and the mv should make any difference at all unless you're running the awk asynchronously (and you have not shown us any script where the awk command is terminated by an ampersand).

Unless something is going on that you have not shown us, adding a sleep will not resolve your issue.