通过Find命令将Unix的Head应用到AWK上
我想输出 find 给出的文件列表中 AWK 命令的前 10 行, 使用此代码片段:
$ 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
I want to output top 10 lines of AWK command in the list of files given by find,
using this snippet:
$ find . -name "*.txt" -print -exec awk '$9 != ""' \| head -n10 {} \;
Note also that I want to print out the file names being processed.
But why I get such error:
awk: cmd. line:2: fatal: cannot open file `|' for reading (No such file or directory)
./myfile.txt
What's the right way to do it?
I tried without backslash before the pipe. Still it gave an error:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当使用 find 的 -exec 运行命令时,您不会获得所有漂亮的 shell 内容,例如管道运算符 (|)。 如果您愿意,您可以通过显式运行子 shell 来重新获得它们,例如:
find 。 -name '*.txt' -exec /bin/sh -c "回显名为 {} 的文本文件 | head -n 15" \;
When running a command with find's -exec, you don't get all the nice shell things like the pipe operator (|). You can regain them by explicitly running a subshell if you like though, eg:
find . -name '*.txt' -exec /bin/sh -c "echo a text file called {} | head -n 15" \;
如果您想对 find 中的每个文件运行 Awk 程序,每次仅打印前 10 行。
If you want to run an Awk program on every file from find that only prints the first 10 lines each time.
根据 Ashawley 的回答:
它应该表现得更好,因为我们在第 10 条记录后退出 awk。
Based on Ashawley's answer:
It should perform better, as we exit awk after the 10th record.
仅使用
awk
应该有效:Using
awk
only should work:您也可以这样做:
没有
head
。You can do it this way too:
without
head
.