分页: 1 / 1

[问题]求教命令管道的一种用法

发表于 : 2007-04-05 13:34
xrfang
我经常使用类似以下的命令:

代码: 全选

ps ax |grep httpd
以上代码检查apache进程是不是在运行。

但我对这种用发不知道对否:

代码: 全选

sudo find / |grep -e \\.keys\$ |grep scite -
以上管道的意图是:第一步,列出出系统所有文件; 第二步,过滤这个列表,只要留下后缀为.keys的文件名;第三步,查找这些文件中有没有含有scite字符串。

关键的疑惑是第三步(前两步是得到我想要的效果的)。第三步的意图是在文件内容里寻找字符串,而不是在文件命列表(即上个管道的输出)里面寻找。

我在一些地方看到了用一个”-“符号的方法,但纯属瞎猜。请高手指点。谢谢。

发表于 : 2007-04-05 13:53
BigSnake.NET
grep searches the named input FILEs (or standard input if no files are
named, or the file name - is given)
for lines containing a match to the
given PATTERN. By default, grep prints the matching lines.

发表于 : 2007-04-05 13:57
laborer
可以用

代码: 全选

locate .keys | xargs grep scite

发表于 : 2007-04-05 14:32
xrfang
谢谢。

我看了xargs,对我这个情况应该是可以用的。但有一个问题。

比如:

locate .txt |xargs cat

假设找到2个文件a.txt, b.txt

xargs就会试图执行命令"cat a.txt b.txt"

有没有办法让它执行"cat a.txt",然后再执行”cat b.txt"? 对于cat可能是一样的,但我觉得会有需要让它分开执行。

发表于 : 2007-04-07 3:04
laborer

代码: 全选

locate .txt | xargs -n 1 cat