怎么把 find 的结果用引号包含起来,然后给其他的命令去处理?

2015-01-05 17:42:06 +08:00
 urmyfaith

怎么把find的结果用引号包含起来,然后给其他的命令去处理?

find . -name "*.m" |  xargs  -n 10 grep -inrE "#define.*http"   > x.txt

思路是这样的:

首先查找到所有.后缀的文件,

然后每次取出10条文件,从文件中查找正则表达式.

最后,将结果重定向到文件中..

但是,这样有一个问题就是,当路径中包含了一个空格的时候,就找不到路径了.

所以,想将find的结果集合中的每一条路径使用引号包起来.

我该怎么做?

2164 次点击
所在节点    macOS
2 条回复
hcymk2
2015-01-05 17:54:42 +08:00
-0 Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). Dis-
ables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU
find -print0 option produces input suitable for this mode.
urmyfaith
2015-01-05 18:24:24 +08:00
@hcymk2

谢谢.解决我的问题了.在man手册也看到了.

```
−0 Change xargs to expect NUL (‘‘\0’’) characters as separators, instead of spaces and newlines.
This is expected to be used in concert with the −print0 function in find(1).

```

原来在设计exargs的时候就考虑到这个问题了.


最后的命令是:

```
find . -name "*.m" -print0 | xargs -n 10 -0 grep -inrE "#define.*http" > x.txt
```

thanks again.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/159440

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX