Home > Software design >  exclude words if the condition is satisfied
exclude words if the condition is satisfied

Time:10-19

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 because a is followed by a, b or c 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]: Match a or b or c
  • .{0,2}: Match 0 to 2 of any characters
  • [a-c]: Match a or b or c

Or else using look arounds in gnu-grep:

grep -P '^(?=.*[a-c])(?!.*[a-c].{0,2}[a-c])' file

ample
taxing
  • Related