python - To print only one occurrence of matching pattern using pcregrep -
is there option in pcregrep allows me print 1 occurrence of matched string pattern? came know option --match-limit. pcregrep not recognizing options. there specific version supports option.
i assume --match-limit=1
prints 1 occurrence of matched pattern.
you can let me know on other possible ways. executing pcregrep command python script via commands utility of python.
before --match-limit, let's review 2 options almost want do.
option 1. when want know if can find match in file, don't care match is, can use -l option so:
pcregrep -l \d\d\d test.txt
where \d\d\d
pattern , test.txt contains strings.
option 2. count number of matches, use
pcregrep -c \d\d\d test.txt
this may closest can want do.
what match--limit
?
--match-limit=1
does work, doesn't want do.
from documentation:
the --match-limit option provides means of limiting resource usage when processing patterns not going match, have large number of possibilities in search trees. classic example pattern uses nested unlimited repeats. internally, pcre uses function called match() calls repeatedly (sometimes recursively). limit set --match-limit imposed on number of times function called during match, has effect of limiting amount of backtracking can take place.
so --match-limit
memory, not number of matches.
let's try out:
if make file called test.txt , add lines 3 digits, so:
111 123 456
then running pcregrep --match-limit=1 \d\d\d test.txt
match these lines.
but if run pcregrep --match-limit=1 \d{3} test.txt
error the resource limit exceeded
.
looking @ full documentation, don't see option limit number of matches. of course design regex so.
for more info
- you know this, short documentation type
pcregrep --help
- the full documentation can downloaded in pcre package pcre.org
- for usage examples, see grep in pcre
Comments
Post a Comment