Reputation: 225
Matching a precise number of characters {min,max}
File name (input_file) contains text as like below:
The Unix operating system was pioneered by Ken Thompson and Dennis Ritchie at Bell Laboratories in the late 1960s.
grep '[A-Za-z]\{4,7\}' input_file
Output: grep command output, matched characters are marked in Bold letters for the references.
The Unix operating system was pioneered by Ken Thompson and Dennis Ritchie at Bell Laboratories in the late 1960s.
After the execution of grep command,
first word Unix:
Total number of word 4, so character Matching Unix with character long 4/4.
second word operating:
Total number of word 9, so character matching operating with character long 7/9.
Third word system:
Total number of word 6, so character matching system character long 6/6.
Fourth word pioneered
Total number of word 9, so character matching pioneered character long 7/9.
Fifth word Thompson
Total number of word 8, so character matchingThompso character long 7/8.
Sixth word Dennis
Total number of word 6, so character matching Dennis character long 6/6.
Here my question is ? Word six having Dennis total number of character 6 but i mentioned {4,7} length long how it will matching entire word, i am not understating here.
Could you please some help me on this?
Upvotes: 0
Views: 84
Reputation: 7560
In the general case (when not using POSIX BRE): Don't escape the {
and }
characters.
[A-Za-z]{4,7}
This will match all continous strings of letters between 4 and 7 in length. The word 'Dennis' is six letter long and evidently a match.
The word 'Laboratories' is really two matches: 'Laborat' and 'ories'.
If what you really wanted is to match strings of length 4 or 7 then you can do this instead:
[A-Za-z]{4}|[A-Za-z]{7}
Upvotes: 2