I have some files in a directory. And I want to add some lines at top and end of file using awk.
Example: My awk command:
awk 'BEGIN { print "line1\nline2" } { print $0 } END { print "line3\nline4" }' file |tee file
By using above command I can add line1 & line2 at the top & line3, line4 at the end of file
Now I want to do same action for all files that are exist in current directory.
If I use :
awk 'BEGIN { print "line1\nline2" } { print $0 } END { print "line3\nline4" }' *
Then I get output on terminal screen but I can't redirect to (or overwrite) all files.
So, I tried following (To find + awk):
find -type f -exec awk 'BEGIN { print "line1\nline2" } { print $0 } END { print "line3\nline4" }' '{}' \;
By using above command I can print output on screen and hence to overwrite files,
I've tried following (To find + awk + overwrite with tee), but it getting error:
$ find -type f -exec awk 'BEGIN { print "line1\nline2" } { print $0 } END { print "line3\nline4" }' '{}' | tee '{}' \;
find: missing argument to `-exec'
Hence, How can I use awk to overwrite (i.e: with |tee or something else) for all files in current directory by command?
sed -i, GNU awk has an in-place editing plugin:gawk -i inplace.-i inplacedoesn't work forBEGINandENDstatements though (works for BEGINFILE, but not ENDFILE)sedinstead ofawk(sed -is -e '1 i\...' -e '$ a\...' *). Decision2 Use loopforfor each file in directory (for file in *; do awk '...' "$file" | tee "$file" ; done).