我如何递归grep目录中每个文件的前50行?


10

我需要搜索目录及其子目录中每个文件的前50行。

这将执行递归部分,但是如何限制每个文件的前50行呢?

grep -r "matching string here" .

其中一些文件很大,我只希望它们在前50行中匹配。我试图通过不在某些文件中搜索兆字节的二进制数据来加快该过程。


您是否只想知道匹配的文件,还是只想拥有匹配的字符串,还是想让匹配的字符串与文件名一起使用?
gniourf_gniourf 2013年

Answers:


11
  • 如果只需要匹配的文件:

    find . -type f -exec bash -c 'grep -q "matching string here" < <(head -n 50 "$1")' _ {} \; -printf '%p\n'
    

    要么

    find . -type f -exec bash -c 'grep -q "matching string here" < <(head -n 50 "$1") && printf '%s\n' "$1"' _ {} \;
    
  • 如果只需要匹配的字符串:

    find . -type f -exec head -n 50 {} \; | grep "matching string here"
    

    或更好,

    find . -type f -exec head -q -n 50 {} + | grep "matching string here"
    
  • 如果您同时想要:

    find . -type f -exec bash -c 'mapfile -t a < <(head -n 50 "$1" | grep "matching string here"); printf "$1: %s\n" "${a[@]}"' _ {} \;
    

备注。

  • sed代替组合head- 可能会稍微容易一些grep
  • 让我强调一下,对于可能包含有趣符号(空格,换行符等)的文件名,所有三种方法都是100%安全的。
  • 在这两种方法中,我假设您有一个不错的bash版本。
  • You could use -exec ... + in each method, but then you'll have to code your inner loop yourself! (trivial exercise left to the reader). This might be very slightly more efficient if you have a gazillion files.

4

If you need the grep output as in the original, you could do:

find . -type f | while read f; do 
  if head -n 50 "$f"|grep -s "matching string here"; then
    grep "matching string here" "$f" /dev/null 
  fi
done

If you only need the file names you can replace the 2nd grep with echo "$f".


1

You'll need to combine a few different utilities to get the desired functionality. Use the find command to recurse the directories, find all files and execute the head command on each file found. The head command can be used to dump only the first 50 lines of each file. Finally, pipe the output to grep to search for your desired string.

find . -type f -exec head -n 50 {} ";" | grep "matching string here"

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.