Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have this following file:-

$ cat numbers
a1
12
12345
123456
19816282
1@$%6

I am using grep "^[0-9]\{1,6\}$" numbers which is giving me the following results:-

12
12345

I was expecting 1@$%6 also in the results. Please correct me if I am wrong?

share|improve this question
    
Try this "^[0-9].*{1,6}$" –  tikend Feb 14 '13 at 13:49
    
it didn't work out. I want to display all lines which have a minimum of 1 digit and a maximum of 6 digits. –  Ankit Feb 14 '13 at 13:54
add comment

2 Answers 2

up vote 3 down vote accepted

The result is actually

12
12345
123456

Your regex means "A line consisting of 1 to 6 digits and nothing else" so 1@$%6 doesn't match.

share|improve this answer
    
+1, is this because I am using $ in my regex? –  Ankit Feb 14 '13 at 13:57
    
Yes, without the $ it would mean "Any line that starts with 1 to 6 digits" which includes 1@$%6. –  Florian Diesch Feb 14 '13 at 14:01
add comment
> grep "[0-9]\{1,6\}" numbers.txt
1
12
12345
123456
19816282
1@$%6
share|improve this answer
    
+1, but $ only specified that matched pattern should end with a number. please correct if I am wrong? –  Ankit Feb 14 '13 at 14:02
1  
the ^"[0-9]\{1,6\}"$ will find pattern of 1 to 6 numbers between beginning and the end of a line. "[0-9]\{1,6\}"$ will find 1 to 6 number before end of a line. –  tikend Feb 14 '13 at 15:54
add comment

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.