2

I was trying to create a regex, to be used in a grep statement to match lines that do not contain a word. In the example below I have the words uhu, uhuu, uhuuu, uhuuu one in each line. I used an echo to do that in one command. Results are:

line1 = uh - end
line2 = uhu - end
line3 = uhuu - end
line4 = uhuuu - end
line5 = uhuuuu - end

Never mind the line number and the end word. So, I wanted to grep uhuu's with 2 and 4 repetitions only, that is line 3 and 5. I tried:

echo -e 'line1 = uh - end\nline2 = uhu - end\nline3 = uhuu - end\nline4 = uhuuu - end\nline5 = uhuuuu - end' | grep -E 'uh(u{2}|u{4})'
line3 = uhuu - end
line4 = uhuuu - end
line5 = uhuuuu - end

It brings line 3 and 5, but also line 4 because uhuuu has 2 u's, plus a 3rd one. So my question is, using Regex, is it possible to exclude that 4th line taking into account only the 'uhuuu' word?

I know I could accomplish this with a pipe and grep -v at the end of everything but I was wondering if this could be done using RegEx.

I checked this link but couldn find a way to make it work for my case. Thank you https://stackoverflow.com/questions/406230/regular-expression-to-match-line-that-doesnt-contain-a-word#=

Community
  • 1
  • 1
Adriano_Pinaffo
  • 1,144
  • 2
  • 13
  • 36

1 Answers1

1

Use a word boundary meta character after grouping:

grep -E 'uh(u{2}|u{4})\b'
Graham
  • 7,035
  • 17
  • 57
  • 82
revo
  • 45,845
  • 14
  • 70
  • 113