urmyfaith
V2EX  ›  macOS

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

  •  
  •   urmyfaith · Jan 5, 2015 · 2673 views
    This topic created in 4165 days ago, the information mentioned may be changed or developed.

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

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

    思路是这样的:

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

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

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

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

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

    我该怎么做?

    2 replies    2015-01-05 18:24:24 +08:00
    hcymk2
        1
    hcymk2  
       Jan 5, 2015   ❤️ 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
       Jan 5, 2015
    @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.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   989 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 5414617a · 32ms · UTC 22:15 · PVG 06:15 · LAX 15:15 · JFK 18:15
    ♥ Do have faith in what you're doing.