gpt4 book ai didi

linux - 通过find命令在AWK上应用Unix的Head

转载 作者:太空狗 更新时间:2023-10-29 11:24:59 24 4
gpt4 key购买 nike

我想在 find 给出的文件列表中输出前 10 行 AWK 命令,使用此代码段:

$ find . -name "*.txt" -print -exec awk '$9 != ""'  \| head -n10 {} \;

另请注意,我想打印出正在处理的文件名。

但为什么会出现这样的错误:

awk: cmd. line:2: fatal: cannot open file `|' for reading (No such file or directory)
./myfile.txt

正确的做法是什么?

我试过在管道前没有反斜杠。仍然报错:

find: missing argument to `-exec'
head: cannot open `{}' for reading: No such file or directory
head: cannot open `;' for reading: No such file or directory

最佳答案

当使用 find 的 -exec 运行命令时,您不会得到所有漂亮的 shell 东西,比如管道运算符 (|)。如果愿意,您可以通过显式运行子 shell 来重新获得它们,例如:

找到。 -name '*.txt' -exec/bin/sh -c "echo a text file called {} | head -n 15"\;

关于linux - 通过find命令在AWK上应用Unix的Head,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/708577/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com