The regex is applied to each line. No matter how you change the regex, if the the whole file contains multiple matching lines then all of them will be printed.
However, you can limit the number of matched lines using the -m
option. -o -m 1
will output at most all matches from one line before exiting. If there are multiple matches in one line use grep ... | head -n1
instead.
Also, keep in mind that .
means any character. To specify a literal dot use .
or [.]
.
Perl regexes also support K
which makes writing easier. Only the part after the last K
will be printed.
grep -Pom1 '.so.Kd.d.d'
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…