Home > other >  Struggling to get a very basic regexp to work [duplicate]
Struggling to get a very basic regexp to work [duplicate]

Time:10-09

I'm on Redhat Enterprise Linux and for some reason I can't get the most basic regexps working on command line. For example, I'm trying to grep all lines with one 'a' in them, so I've been trying:

grep [a]{1} <fileName>  
grep '[a]{1}' <fileName>  
grep '[a]\{1\}' <fileName>   
grep 'a{1}' <fileName>  

etc, and none of them are working. This seems to identically match the syntax I'm reading everywhere so I'm quite confused as to where I'm going wrong, any advice would be appreciated.

CodePudding user response:

You're matching lines with at least one a in them. Even though you have {1} in the regexp, it matches anywhere on the line. So a line with two a will still match bceause it matches the first one.

You need to exclude a in the rest of the line:

grep '^[^a]*a[^a]*$' filename
  • Related