Karl Karl - 3 months ago 20
Linux Question

How can i execute awk script with find command on multiple files

This command is working fine for me for single file

awk -f test.awk template.yml file.yml > tmp && mv tmp file.yml


The fins command i have find all files i want

find . -wholename "*vars/*.yml"


I am not sure how can i execute above awk command on all files so that they can get replaced in place. I have old version of awk 3.1

Answer

GNU awk has inplace modification option, if you have GNU awk:

find . -wholename "*vars/*.yml" -exec awk -i inplace -f test.awk {} +

Otherwise, you need to handle the files one at a time:

find . -wholename "*vars/*.yml" -exec \
     bash -c 'awk -f test.awk "$1" >"$1".tmp && mv "$1".tmp "$1"' _ {} \;