unix – 如何在-exec中使用管道查找

栏目: 服务器 · 发布时间: 8年前

内容简介:http://stackoverflow.com/questions/21825393/how-to-use-pipe-within-exec-in-find

有没有办法在-exec中使用管道进行查找?我不想让grep通过整个文件,只能通过每个文件的第一行.

find /path/to/dir -type f -print -exec grep yourstring {} \;

我试图将管道放在“猫”和“头-1”,但没有很好的工作.我试图用某种方式使用括号,但是我并没有想出如何把它们放在那里.

我非常感谢你的帮助.我知道如何以其他方式工作,没有使用发现,但我们试图在学校使用查找和管道,但不能管理如何.

find /path/to/dir -type f -print -exec cat {} | head -1 | grep yourstring \;

这是不知何故我们试图做的,但是不能管理括号,甚至是可能的.我试图通过网络来看,但是找不到任何答案.

为了能够使用管道,您需要执行一个 shell 命令,即带有管道的命令必须是-exec的单个命令.

find /path/to/dir -type f -print -exec sh -c "cat {} | head -1 | grep yourstring" \;

请注意,以上是一个无用的猫,可以写成:

find /path/to/dir -type f -print -exec sh -c "head -1 {} | grep yourstring" \;

实现你想要的另一种方法就是说:

find /path/to/dir -type f -print -exec awk 'NR==1 && /yourstring/' {} \;

http://stackoverflow.com/questions/21825393/how-to-use-pipe-within-exec-in-find


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

The Black Box Society

The Black Box Society

Frank Pasquale / Harvard University Press / 2015-1-5 / USD 35.00

Every day, corporations are connecting the dots about our personal behavior—silently scrutinizing clues left behind by our work habits and Internet use. The data compiled and portraits created are inc......一起来看看 《The Black Box Society》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试