grep - Find word that has more than 50 characters in all php files

07
2014-07
  • Hommer Smith

    I am trying to use find in combination with grep to find all the php files in my web server which have a word (any sequence of characters except whitespace) with more than 50 characters.

    I have this base:

    find . -name '*.php' -exec fgrep -q '.{50}' {} \; -print
    

    But is not working as expected.

    Thanks

  • Answers
  • gronostaj

    . stands for everything except for newline characters, including spaces and tabs. That's why your regular expression will match lines longer than 50 characters.

    To match words that are at least 50 characters long you can use something like this:

    [^\s]{50}
    

    [^\s] matches everything except for whitespace characters. {50} means that sequence of 50 such (not necessarily identical) characters will be matched.


  • Related Question

    linux - Find words in many files
  • ant2009

    I am looking for this struct messages_sdd_t and I need to search through a lot of *.c files to find it. However, I can't seen to find a match as I want to exclude all the words 'struct' and 'messages_sdd_t'. As I want to search on this only 'struct messages_sdd_t' The reason for this is, as struct is used many times and I keep getting pages or search results.

    I have been doing this without success:

    find . -type f -name '*.c' | xargs grep 'struct messages_sdd_t'
    

    and this

    find . -type f -name '*.c' | xargs egrep -w 'struct|messages_sdd_t'
    

    Many thanks for any suggestions,


  • Related Answers
  • Ivan Petrushev

    Use ack:

    ack "struct messages_sdd_t" *.c

    It is recoursive by default and it is a lot faster than grep (according to my observation) when searching trough a directory containing tons of source code.

    If you don't want to use ack, grep should be fine too:

    grep -r "struct messages_sdd_t" *.c

  • Ignacio Vazquez-Abrams
    find . -type f -name '*.c' -exec grep 'struct\s\+messages_sdd_t' {} \;
    

    Or if you only care about the filename:

    find . -type f -name '*.c' -exec grep -q 'struct\s\+messages_sdd_t' {} \; -print
    
  • Daniel Andersson
    grep -R --include='*.c' 'struct messages_sdd_t' .
    

    This will search recursively and use grep itself to pick only .c files.

    Consider adding -H to always write the file name with the line match.