Is there a grep equivalent for find's -print0 and xargs's -0 switches?

前端 未结 6 902
小蘑菇
小蘑菇 2020-12-04 23:36

I often want to write commands like this (in zsh, if it\'s relevant):

find  | \\
    grep stringinfilenamesIwant | \\
          


        
相关标签:
6条回答
  • 2020-12-05 00:02

    Use GNU Grep's --null Flag

    According to the GNU Grep documentation, you can use Output Line Prefix Control to handle ASCII NUL characters the same way as find and xargs.

    -Z
    --null
    Output a zero byte (the ASCII NUL character) instead of the character that normally follows a file name. For example, ‘grep -lZ’ outputs a zero byte after each file name instead of the usual newline. This option makes the output unambiguous, even in the presence of file names containing unusual characters like newlines. This option can be used with commands like ‘find -print0’, ‘perl -0’, ‘sort -z’, and ‘xargs -0’ to process arbitrary file names, even those that contain newline characters.

    Use tr from GNU Coreutils

    As the OP correctly points out, this flag is most useful when handling filenames on input or output. In order to actually convert grep output to use NUL characters as line endings, you'd need to use a tool like sed or tr to transform each line of output. For example:

    find /etc/passwd -print0 |
        xargs -0 egrep -Z 'root|www' |
        tr "\n" "\0" |
        xargs -0 -n1
    

    This pipeline will use NULs to separate filenames from find, and then convert newlines to NULs in the strings returned by egrep. This will pass NUL-terminated strings to the next command in the pipeline, which in this case is just xargs turning the output back into normal strings, but it could be anything you want.

    0 讨论(0)
  • 2020-12-05 00:10

    As you are already using GNU find you can use its internal regular expression pattern matching capabilities instead of these grep, eg:

    find <somebasedirectory> -regex ".*stringinfilenamesIwant.*" ! -regex ".*stringinfilesnamesIdont.*" -exec dosomecommand {} + 
    
    0 讨论(0)
  • 2020-12-05 00:17

    The newest version of the GNU grep source can now use -z/--null to separate the output by null characters, while it previously only worked in conjunction with -l:

    http://git.savannah.gnu.org/cgit/grep.git/commit/?id=cce2fd5520bba35cf9b264de2f1b6131304f19d2

    This means that your issue is solved automatically when using the newest version.

    0 讨论(0)
  • 2020-12-05 00:17

    Use

    find <somebasedirectory> -print0 | \
     grep -z stringinfilenamesIwant | \
     grep -zv stringinfilesnamesIdont | \
     xargs -0 dosomecommand
    

    However, the pattern may not contain newline, see bug report.

    0 讨论(0)
  • 2020-12-05 00:24
    find <somebasedirectory> -print0 | xargs -0 -I % grep something '%'
    
    0 讨论(0)
  • 2020-12-05 00:26

    Instead of using a pipe, you can use find's -exec with the + terminator. To chain multiple commands together, you can spawn a shell in -exec.

    find ./ -type f -exec bash -c 'grep "$@" | grep -v something | xargs dosomething' -- {} +
    
    0 讨论(0)
提交回复
热议问题