Search code examples
bashgrepxargs

How to make this script grep only the 1st line


for i in USER; do
    find /home/$i/public_html/ -type f -iname '*.php' \
    | xargs grep -A1 -l 'GLOBALS\|preg_replace\|array_diff_ukey\|gzuncompress\|gzinflate\|post_var\|sF=\|qV=\|_REQUEST'
done

Its ignoring the -A1. The end result is I just want it to show me files that contain any of matching words but only on the first line of the script. If there is a better more efficient less resource intensive way that would be great as well as this will be ran on very large shared servers.


Solution

  • The following code is untested:

    for i in USER; do
        find /home/$i/public_html/ -type f -iname '*.php' | while read -r; do
            head -n1 "$REPLY" | grep -q 'GLOBALS\|preg_replace\|array_diff_ukey\|gzuncompress\|gzinflate\|post_var\|sF=\|qV=\|_REQUEST' \
                && echo "$REPLY"
        done
    done
    

    The idea is to loop over each find result, explicitly test the first line, and print the filename if a match was found. I don't like it though because it feels so clunky.