-1

I am required to find all lines that start with a capital letter AND ends with the word dog followed by a period.

I know that to find lines starting with a capital letter I would do: egrep ^[A-Z] filename

And to find lines that end with "dog." I would do: egrep dog.$ filename

However how would I do them both together? I have tried: egrep ^[A-Z]*dog.$ filename

but it does not seem to work. In filename substitution the * character represented a string of any length, or no string at all even.

2 Answers 2

4

You probably need this:

grep '^[[:upper:]].*dog\.$' file

You probably already know the anchors ^ (for beginning of line) and $ (for end of line). Instead of using [A-Z], you'd better use the class [[:upper:]] as this might better deal with accentuated capital letters (mind you, if your question is for homework, your teacher might not be aware of this — my experience is that command line courses at schools is one of the worst teaching that exists, teachers are usually incompetent at this as they use ancient techniques... (bad) habits die hard!).

Then you're looking for .* where the period . matches any character and then .* means any number of any character.

Finally, you need an escaped period \. otherwise a line ending with dogs or dog! would match... since an unnescaped period matches any character.

Hope this helps!

1
0

You need to write the following pattern: [A-Z].*dog.

[A-Z] expects a capital letter dot means that it is any character. The way you have the '*' means that as many capital letters. If you want alphabet characters then instead of '.' put [a-z]. The star doesn't work as as a wildcard like in bash command line, but it rather takes into account the previous character.

I hope this was helpful

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .