1

I want to match strings that have less or equal to 2 digits, but not more than 2 digits. If the digits are not consecutive, they could be more than 2 digits in total, but each consecutive digits should be at most 2 letters.

So, I want to match strings like

42
ABC92
05XYZ
ABC82XYZ
()%^.90 XYZ
AB 47.jpg
3
83abc29
9abw88ak ak8+

but not

426
ABC9242
05697XYZ
ABC8232XYZ
()%^.9068 XYZ
AB 479.jpg
483 a8

The letter before or after the digits can be not only alphabets, but also any characters that are not digit (No letters before or after the digits can be possible, and such strings should be matched, too).

.*[[:digit:]]{1,2}.* matches strings that have more than 2 digits, too. This is not what I want.

How can I do this?

4
  • what about ABC2dsd2 should it match ? Commented Mar 27, 2014 at 6:01
  • @aelor sorry, it should be matched Commented Mar 27, 2014 at 6:05
  • @Naetmul Please clarify. If it should match even if not consecutive, as in the case pointed out by aelor, shouldn't you remove the phrase "consecutive digits" from the question statement? Commented Mar 27, 2014 at 6:26
  • @John 1024 ok. I will make it more clear. Commented Mar 27, 2014 at 6:29

1 Answer 1

5

Using only bash. Rather than trying to find anything with fewer than 3 digits, find whatever matches 3 digits or more and exclude it:

while read -r line; do
  [[ "$line" =~ [[:digit:]][[:digit:]][[:digit:]] ]] || echo "$line"
done << EOF
42
ABC92
05XYZ
ABC82XYZ
()%^.90 XYZ
AB 47.jpg
426
ABC9242
05697XYZ
ABC8232XYZ
()%^.9068 XYZ
AB 479.jpg
EOF

Produces:

42
ABC92
05XYZ
ABC82XYZ
()%^.90 XYZ
AB 47.jpg

Explanation: The binary operator =~ considers the RHS as an extended regular expression. It matches anything that contains 3 digits (or more). The || is a short-circuit operator:

expression1 || expression2

which implies that expression2 is executed only when expresssion1 is false.

As such, only lines that do not contain 3 or more consecutive digits are printed.


You could also use grep using a flag to enable extended regular expressions or PCRE:

grep -vE '[0-9]{3,}' inputfile

or

grep -vP '[0-9]{3,}' inputfile
Sign up to request clarification or add additional context in comments.

1 Comment

@aelor Edited the answer to explain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.