I have this text file and I want to exclude the word "access" because a is followed by a, b or c at second, third or forth position.
# cat tt.txt
access
ample
taxing
I tried this, but it returns all 3 words.
# grep '[a-c][^a-c][^a-c][^a-c]' tt.txt
access
ample
taxing
Update 1:
I used over-simplified example above.
# cat tt.txt
access
bccess
ample
taxing
tacking
not
# grep -Ev '[a-c].{0,2}[a-c]' tt.txt
ample
taxing
not
# grep -E '[a-c].{0,2}[^a-c]' tt.txt
access
bccess
ample
taxing
tacking
# Expected
ample
taxing
CodePudding user response:
I want to exclude the word
access
becausea
is followed bya
,b
orc
at second, third or forth position
It can be done using this awk
:
awk '/[a-c]/ && !/[a-c].{0,2}[a-c]/' file
ample
taxing
RegEx Breakdown:
[a-c]
: Matcha
orb
orc
.{0,2}
: Match 0 to 2 of any characters[a-c]
: Matcha
orb
orc
Or else using look arounds in gnu-grep
:
grep -P '^(?=.*[a-c])(?!.*[a-c].{0,2}[a-c])' file
ample
taxing