Issue
I have two regular expression, and trying to bind it into And condition
what I got
-grep -E "/[1-9]{4,}/" file
-grep -E '([0-9])(.*\1){3}' file
I tried to take a regular expression from each command, then bind it with multiple grep with pipe
cat file | grep pattern1 | grep patterns
, but didn't work.
anyone can teach me way to use and condition for grep with these two patterns?
"/[1-9]{4,}/" '([0-9])(.*\1){3}'
sample input
Q4HXD/7100525/+wg4C54V2I4mh4Xh
aaaa/123/422444qjem,,qewriiafa
!#@AVADFQWERASDFASDFQervzxcilh
expected output
Q4HXDa /7100525/+wg4C54V2I4mh4Xh
which satisfy both condition
Solution
You need to use [0-9]
or [[:digit:]]
to match any digit in a POSIX pattern and make sure both patterns are handled as POSIX ERE by passing -E
option:
cat file | grep -E '/[0-9]{4,}/' | grep -E '([0-9])(.*\1){3}'
Else, you may use a PCRE pattern like
grep -P '^(?=.*/[0-9]{4,}/).*([0-9])(.*\1){3}' file
See an online grep demo
The latter pattern matches
^
- start of a string(?=.*/[0-9]{4,}/)
- a positive lookahead that makes sure there is/
, 4 or more digits,/
after any 0+ chars other than line break chars.*
- any 0+ chars other than line break chars, as many as possible([0-9])
- Group 1: any digit(.*\1){3}
- three occurrences of any 0+ chars other than line break chars, as many as possible, and then the Group 1 value.
Answered By - Wiktor Stribiżew