I have come across a question(on SO itself) where OP has to do edit and save operation into Input_file(s) itself.
I know for a single Input_file we could do following:
awk '{print "test here..new line for saving.."}' Input_file > temp && mv temp Input_file
Now lets say we need to make changes in same kind of format of files(assume .txt here).
What I have tried/thought for this problem: Its approach is going through a for loop of .txt files and calling single awk
is a painful and NOT recommended process, since it will waste unnecessary cpu cycles and for more number of files it would be more slow.
So what possibly could be done here to perform inplace edit for multiple files with a NON GNU awk
which does not support inplace option. I have also gone through this thread Save modifications in place with awk but there is nothing much for NON GNU awk vice and changing multiple files inplace within awk
itself, since a non GNU awk will not have inplace
option to it.
NOTE: Why I am adding bash
tag since, in my answer part I have used bash commands to rename temporary files to their actual Input_file names so adding it.
EDIT: As per Ed sir's comment adding an example of samples here, though purpose of this thread's code could be used by generic purpose inplace editing too.
Sample Input_file(s):
cat test1.txt
onetwo three
tets testtest
cat test2.txt
onetwo three
tets testtest
cat test3.txt
onetwo three
tets testtest
Sample of expected output:
cat test1.txt
1
2
cat test2.txt
1
2
cat test3.txt
1
2
Since main aim of this thread is how to do inplace SAVE in NON GNU
awk
so I am posting first its template which will help anyone in any kind of requirement, they need to add/appendBEGIN
andEND
section in their code keeping their main BLOCK as per their requirement and it should do the inplace edit then:NOTE: Following will write all its output to output_file, so in case you want to print anything to standard output please only add
print...
statement without> (out)
in following.Generic Template:
Specific provided sample's solution:
I have come up with following approach within
awk
itself (for added samples following is my approach to solve this and save output into Input_file itself)NOTE: this is only a test for saving edited output into Input_file(s) itself, one could use its BEGIN section, along with its END section in their program, main section should be as per the requirement of specific question itself.
Fair warning: Also since this approach makes a new temporary out file in path so better make sure we have enough space on systems, though at final outcome this will keep only main Input_file(s) but during operations it needs space on system/directory
Following is a test for above code.
Execution of program with an example: Lets assume following are the
.txt
Input_file(s):Now when we run following code:
NOTE: I have place
ls -lhtr
insystem
section intentionally to see which output files it is creating(temporary basis) because later it will rename them into their actual name.When we do a
ls -lhtr
afterawk
script is done with running, we could see only.txt
files in there.Explanation: Adding a detailed explanation of above command here: