Issue
I would like to implement a regular expression in linux that using grep allows me to verify that a field contains 15 numerical values and that the value occupying the fifth position (starting from left) is either a 5 or a 6.
I have reached the point of defining the requirement that it contains a maximum of 15 values, however, I can not get that the one that occupies the fifth position is a 5 or 6. It would be:
grep -E "^[0-9]{1,15}"
Any idea?
Solution
For exactly 15 numbers, and the 5 position is either 5 or 6:
grep -E "^[0-9]{4}[56][0-9]{10}$"
^
Start of string[0-9]{4}
Match 4 digits[56]
Match either5
or6
[0-9]{10}
Match 10 digits$
End of string
To match at least the first 5 characters followed by 0-10 digits after it, and allow a partial match like matching 123462222233333
in 12346222223333344444
grep -Eo "^[0-9]{4}[56][0-9]{0,10}"
Answered By - The fourth bird Answer Checked By - Dawn Plyler (WPSolving Volunteer)