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

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

内容简介: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


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

查看所有标签

猜你喜欢:

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

Database Design and Implementation

Database Design and Implementation

Edward Sciore / Wiley / 2008-10-24 / 1261.00 元

* Covering the traditional database system concepts from a systems perspective, this book addresses the functionality that database systems provide as well as what algorithms and design decisions will......一起来看看 《Database Design and Implementation》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具