Search code examples
regexlinuxgrepfindpcregrep

How to find files with text started with


I tried to use

find ./ -type f -name '*.php' -exec grep -slE '^asdasd' {} \;

and

find ./ -type f -name '*.php' -exec pcregrep -l '^asdasd' {} \;

But this commands found files, where 'asdasd' in beginning of lines, not of all text, for example:

File content:

qweqwe

asdasd

czczc

I want to find files only with this file content:

asdasd

qwdq

qwdad

(asdasd in beginning of all text)


Solution

  • With awk you can check if the first line matches whatever saying:

    awk 'NR==1 && /pattern/' file
    

    If you want to print its file name, then say:

    awk 'NR==1 && /pattern/ {print FILENAME}'
    

    To combine it with find and check if the first line starts with "asdasd", use:

    find -type f -exec awk 'NR==1 && /^asdasd/ {print FILENAME}' {} \;