Reputation: 33
My pattern needs to have three words where there is a single SPACE character separating the three tokens in each line.
for example
inherit class3 class4
inherit class35 class5
implement class3 interface1
aggregate class3 class5
My code is egrep -i "^[0-9a-z]+\ [0-9a-z]+\ [0-9a-z]+$" "$input2"
This still does not match it properly and outputs nothing with $ at the end, but if i remove the $, it outputs the lines having more than 3 tokens in each line. I've been trying to make it work perfectly for a really long time but its not working. Thanks.
Test file has this
d d d d
dfsdfg afegsdfh asfg
efgfah afgadfg adfsg
awg awg wga
wrhg warg wrgrw
awdg afsg
sdg
asdg asdg asdg asdg
Upvotes: 0
Views: 423
Reputation: 12316
Your test file doesn't seem to have any patterns that exactly match the search... there is a space at the end of the one line that would match.
To solve, add some optional spaces in your search with *
...
Upvotes: 1
Reputation: 4500
Given the test data, no lines will match the regex. It is behaving as it should. You might think it will match the third line, but it can't because there's a trailing space, so the third token is not immediately followed by the end of line.
When you remove the end of line character from the regex pattern, it indeed does not have this constraint, so, as long as the line begins with three tokens separated by a space, it will be matched. Therefore, the lines, as described by the asker, would print out.
Upvotes: 1
Reputation: 2050
Try this:
egrep -i "^\ *[0-9a-z]+\ [0-9a-z]+\ [0-9a-z]+\ *$"
The command you tried was not working because it doesn't capture the cases when there are spaces in the beginning or end.
Upvotes: 0
Reputation: 6181
$ is a a regex symbol for end of line. Therefore if you want something to end with $ you should you backslash (as you use near the brackets) - \$. As I see it your command should actually end with \$$
Upvotes: 0