CODrenia CODrenia - 18 days ago 13
Linux Question

Find a regular expression that searches for a specific pattern in a file (Unix-Solaris)

I have this file. Its a number at the start, then a name and a surname (someone can have 2 names or/and 2 surnames but not more)

21501 Sylvester Stallone
21502 Tommy Lee Jones
21503 Jean Claude Van Damme


I have to use
grep
. So far I have thought these 2 option I'm almost 100% sure none of them is right but it's a start.

grep -e [0-9]\{5\}[[:space:]][A-Z][a-z]

grep '^([a-z]+)[ \s]([a-z]+)/n'

Answer
grep -E -i '^[0-9]{5}( [a-z]+){2,4}$' filename

^ anchors the pattern to the beginning of the line. Then it looks for 5 digits, followed by 2-4 names each preceded by a space. $ anchors it to the end of the line.

-E makes it use extended regular expressions, and -i makes it case-insensitive so you don't need to say [A-Za-z].

Comments