Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Why in the following:

echo this is a line. | grep -o -E "[a-z]+\."

The result is line. and not this is a line.?

There is no word boundary or any other restriction on the regex.

share|improve this question

2 Answers 2

up vote 5 down vote accepted

[a-z] only matches the characters a to z. they do not match the space character. when you want to match also the space character you have to explicitly say so: [a-z ].

share|improve this answer
    
[a-z] only matches the characters a to z. Yes I understand.But why in this case it is line that matches and not this or is or this is a line? –  Jim Jul 8 '13 at 5:59
    
because your pattern has a dot in the end. line. is the only part that matches "at least one of a to z followed by a dot". –  lesmana Jul 8 '13 at 8:04

The -o option instructs grep to limit the output to just that part of the line which matches the pattern.

Rerun the command without -o and the result will indeed be

this is a line.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.