CLI tip 21: inplace file editing with GNU awk
You can use the -i
option with GNU awk
to load libraries. The inplace
library comes by default with the GNU awk
installation. Thus, you can use -i inplace
to modify the original input itself. Make sure to test that the code is working as intended before using this option.
$ cat table.txt
brown bread mat cake 42
blue cake mug shirt -7
yellow banana window shoes 3.14
# retain only the first and third fields
$ awk -i inplace '{print $1, $3}' table.txt
$ cat table.txt
brown mat
blue mug
yellow window
You can provide a backup extension by setting the inplace::suffix
special variable. For example, if the input file is ip.txt
and inplace::suffix='.orig'
is used, the backup file will be named as ip.txt.orig
.
$ cat marks.txt
Name Physics Maths
Moe 76 82
Raj 56 64
$ awk -i inplace -v inplace::suffix='.bkp' -v OFS=, '{$1=$1} 1' marks.txt
$ cat marks.txt
Name,Physics,Maths
Moe,76,82
Raj,56,64
# original file is preserved in 'marks.txt.bkp'
$ cat marks.txt.bkp
Name Physics Maths
Moe 76 82
Raj 56 64
Earlier versions of GNU awk
used INPLACE_SUFFIX
variable instead of inplace::suffix
. Also, you can use inplace::enable
variable to dynamically control whether files should be inplaced or not. See gawk manual: Enabling In-Place File Editing for more details.
See this unix.stackexchange thread for details about security implications of using the -i
option and workarounds.
Video demo:
See my CLI text processing with GNU awk ebook if you are interested in learning about the GNU awk
command in more detail.