Skip to content
Advertisement

search numbers with egrep and regular expression

Given a file file1 with this text:

Anah, Julio, 5-3871 Ghin, Ermen, 4-4123 Jones, Thomas, 1-4122 Solor, R]ehard, 5-2522 Salazar, Hex, 8-2321 Jones, Tommy, 5-2911 Kar, Rt, 3-3633.

I want to use egrep to search all the numbers that start with 5 and end with 1.

I have tried:

grep '5' '1' file1

Advertisement

Answer

This regex will work

egrep -o \b5[0-9-]*1\b inputfile

Regex Breakdown

\b # word boundary
5 #Number starts with 5
[0-9-]* #Any combination of 0-9 and -
1 # Ends with 1
\b Word boundary
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement