Stop grep after matching pattern, not a whole line? Files with a long line / single line

I use grep to return only a line between the pattern (-o) within multiple files, e.g.

grep -i -r -o 'Rows="[^#][^"]*"' *

I would like it to stop after the first matching pattern, I tried grep -m NUM / -max-count = NUM ​​but this stops reading the file after NUM matching lines, not the pattern, and unfortunately all the files in this folder are one long line.

Can grep stop after the first matching pattern, as an alternative, should I do this with something else?

* Bonus question - Ability to display it as a "Consolidated template" "File name" so that I can sort it?

+3
source share
3 answers

, grep . , Unix . , sed, . awk, , - , , .

GNU awk , :

gawk 'BEGIN {RS="pattern"} {print RT, FILENAME; exit}' filename

RS ( ) . RT - , RS. FILENAME . exit . , awk . , .

, find xargs , -:

find . -type f -print0 | xargs -0 gawk 'BEGIN {RS="pattern"} {print RT, FILENAME; if (RT != "") exit}'

( ) , , , .

, : , gawk , , ( , 490 .)

0

:

perl -nle '/(Rows="[^#][^"]*")/ or continue; print $ARGV, ":", $1; exit 0' files ...

, , . , grep, - , :

for file in *; do
    # Replace every R with newline,
    # and every newline with dot.
    # Your tr syntax for newline may be different
    tr 'R\n' '\n.'  < "$file" |
    sed -n '/^\(ows="[^#][^"]*"\).*/{;s%%'"$file:"'R\1%;p;q;}'
done

tr sed , .

: , grep sed.

0

I have not tested it, but I would try:

find -type f -print0 | xargs -0 -r cat | grep -m 1 -i -o 'Rows="[^#][^"]*"'
-1
source

All Articles