V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
urmyfaith
V2EX  ›  macOS

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

  •  
  •   urmyfaith · 2015-01-05 17:42:06 +08:00 · 2155 次点击
    这是一个创建于 3410 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    思路是这样的:

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

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

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

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

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

    我该怎么做?

    2 条回复    2015-01-05 18:24:24 +08:00
    hcymk2
        1
    hcymk2  
       2015-01-05 17:54:42 +08:00   ❤️ 1
    -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
        2
    urmyfaith  
    OP
       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.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1027 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 19:56 · PVG 03:56 · LAX 12:56 · JFK 15:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.