I am trying to create an egrep command to grep the number of lines containing a specific text from a text-file but seem to have an error somewhere.
The text-file contains several thousand lines and has the expression Lastname
in several lines. Problem is, there are also expressions like xLastname
or abcLastname
in there which I DON'T want to grab.
So the definition of the RegExpression should look like this:
EITHER there is no text at all in the line before Lastname
appears
OR there is text in the line BUT a space has to be between the random text and Lastname
I tried with ((.+\ )?|(^.))Lastname
and ((.+\ )?|[^.])Lastname
but it always results in the egrep command finding expressions like abcLastname
.
Where is my mistake? The first part (.+\ )
("If there is text, there HAS to be a space afterwards") seems to work fine but the second part ("OR there is no text at all") does not seem to work.
Isn't there a special character that I can use to simply check if whatever comes after it is the FIRST expression/character in the line? I cannot find that information online unfortunately.
Thanks to all of you in advance for your help
grep -E '\bLastname\b'
grep -w Lastname
(-w
means: "Select only those lines containing matches that form whole words.").