Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash regex for word with some suffixes but not one specific

Tags:

regex

grep

bash

I need (case-insensitive) all matches of several variations on a word--except one--including unknowns.

I want

accept
acceptance
acceptable
accepting

...but not "acception." A coworker used it when he meant "exception." A lot.

Since I can't anticipate the variations (or typos), I need to allow things like "acceptjunk" and "acceptMacarena"

I thought I could accomplish this with a negative lookahead, but this didn't give the results I needed

grep -iE '(?!acception)(accept[a-zA-Z]*)[[:space:]]' file

The trick is that I can accept (har) lines that contain "acception," provided that the other words match. For example this line is okay to match:

The acceptance of the inevitable is the acception.

...otherwise by now I'd have piped grep through grep -v and been done with it:

grep -iE '(accept)[a-zA-Z]*[[:space:]]' | grep -vi 'acception'

I've found some questions that are similar and many that are not quite so. Using a-zA-Z is likely unnecessary in grep -i but I'm flailing. I'm probably missing something small or basic...but I'm missing it nonetheless. What is it?

Thanks for reading.

PS: I'm not married to grep--but I am operating in bash--so if there's a magic awk command that would do this I'm all ears (eyes).

PPS: forgot to mention that on https://regex101.com/ the above lookahead seemed to work, but it doesn't with my full grep command.

like image 937
zedmelon Avatar asked Feb 26 '18 04:02

zedmelon


1 Answers

To use lookarounds, you need GNU grep with PCRE available

grep -iP '(?!acception)(accept[a-z]*)[[:space:]]'


With awk, this might work

awk '{ip=$0; sub(/acception/, ""); if(/accept[a-zA-Z]*[[:space:]]/) print ip}'
  • ip=$0 save input line
  • sub(/acception/, "") remove unwanted words, can add other unwanted words with alternation
  • if(/accept[a-zA-Z]*[[:space:]]/) print ip then print the line if it still contains words being searched
like image 161
Sundeep Avatar answered Oct 20 '22 16:10

Sundeep