awkin-place

How to create AWK scripts with shebang that allows in-place modifications?


I want create a awk script with a shebang such that

./main.awk text.txt

performs in-place modification in text.txt. Inspired by a previous solution, I tried something like

#!/usr/bin/awk -i inplace -f

but it didn't work.


Solution

  • Use /usr/bin/env with the -S option to process multiple arguments:

    #!/usr/bin/env -S awk -i inplace -f
    

    See man env:

    -S, --split-string=S
           process and split S into separate arguments; used to pass multiple arguments on shebang lines