Write a shell script that finds-greps and prints the file name and contents in 1 line

To view all php files containing "abc", I can use this simple script:

find . -name "*php" -exec grep -l abc {} \;

I can omit -l and I extract some of the content instead of the file names as the results:

find . -name "*php" -exec grep abc {} \;

Now I need a version that works simultaneously, but on the same line.

Expected Result:

path1/filename1: lorem abc ipsum
path2/filename2: ipsum abc lorem
path3/filename3: non abc quod

More or less like it grep abc *.

Edit: I want to use this as a simple shell script. It would be great if the exit were on the same line, so further matching would be possible. But it is not necessary that the script be just one line, I still put it in the bash script file.

2: "ack", , grep. . http://betterthangrep.com/ ack --php --nogroup abc,

+3
3

-H (man grep):

find . -name "*php" -exec grep -H abc {} \;

xargs ( -H , grep ):

find . -name "*php" -print | xargs grep abc

: grep, orsogufo, -H find , , (.. PHP). orsogufo w.r.t. -print0, :

find . -name "*php" -print0 | xargs -0 grep -H abc

2: A ( 1) POSIX , /dev/null -H:

find . -name "*php" -print0 | xargs -0 grep abc /dev/null

1: opengroup.org find , -print0 :

SVR4 -exec + . , ( s) , , xargs. , -print0 primary, . , . , , find -print0 output .

+7

, .

grep -H abc *.php

.., . -H - ( , OSG OS X), :

grep abc *.php

grep -R, .php :

grep -R abc *

, .

, , ... ... grep , , find/-exec/grep/xargs ! ( script, )

+2
find /path -type f -name "*.php" | awk '
{
    while((getline line<$0)>0){
        if(line ~ /time/){
            print $0":"line
            #do some other things here
        }
    }    
}'
+1

Source: https://habr.com/ru/post/1711876/


All Articles