Pan*_*dya 1 find io-redirection awk shell-script
我在一个目录中有一些文件。我想使用awk.
示例:我的awk命令:
awk 'BEGIN { print "line1\nline2" } { print $0 } END { print "line3\nline4" }' file |tee file
Run Code Online (Sandbox Code Playgroud)
通过使用上面的命令,我可以添加line1&line2在顶部 & line3,line4在末尾file
现在我想对当前目录中存在的所有文件执行相同的操作。
如果我使用:
awk 'BEGIN { print "line1\nline2" } { print $0 } END { print "line3\nline4" }' *
Run Code Online (Sandbox Code Playgroud)
然后我在终端屏幕上得到输出,但我无法重定向到(或覆盖)所有文件。
所以,我尝试了以下 (To find+ awk):
find -type f -exec awk 'BEGIN { print "line1\nline2" } { print $0 } END { print "line3\nline4" }' '{}' \;
Run Code Online (Sandbox Code Playgroud)
通过使用上面的命令,我可以在屏幕上打印输出,从而覆盖文件,
我试过以下(To find++ awkoverwrite with tee),但出现错误:
$ find -type f -exec awk 'BEGIN { print "line1\nline2" } { print $0 } END { print "line3\nline4" }' '{}' | tee '{}' \;
find: missing argument to `-exec'
Run Code Online (Sandbox Code Playgroud)
因此,如何使用awk,以覆盖(即:用|tee或别的东西)的所有文件通过命令在当前目录?
使用 GNU awk4.1 或更高版本:
find . -type f -exec awk '
@load "inplace"
BEGINFILE {
inplace_begin(FILENAME, "")
print "line1\nline2"
}
{print}
ENDFILE {
print "line3\nline4"
inplace_end(FILENAME, "")
}' {} +
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
963 次 |
| 最近记录: |