linux - [Solved-5 Solutions] Awk save modifications in place - ubuntu - red hat - debian - linux server - linux pc
Linux - Problem :
If there is an option to write changes to file, similar to sed where you would use -i option to save modifications to a file.
You could use redirection to write changes. However is there an option in awk to do that?
Linux - Solution 1:
In latest GNU Awk , it has the option of "inplace" file editing:
The "inplace" extension, built using the new facility, can be used to simulate the GNU "sed -i" feature.
Example usage:
To keep the backup:
Linux - Solution 2:
Unless you have GNU awk 4.1.0
You won't have such an option as sed's -i option so instead do:
Note: The -i is not magic, it is also creating a temporary file sed just handles it for you.
As of GNU awk 4.1.0
The new -i option (from xgawk) is used for loading awk library files. This differs from -f in that the first non-option argument is treated as a script.
You need to use the bundled inplace.awk include file to invoke the extension properly like so:
The variable INPLACE_SUFFIX can be used to specify the extension for a backup file:
Linux - Solution 3:
- The shell performs the redirections before handing control over to someprocess (redirections).
- The > redirection will truncate the file to zero size (redirecting output). Therefore, by the time someprocess gets launched and wants to read from the file, there is no data for it to read.