Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting a substring from a regular expression

Tags:

regex

grep

Here is the line of text:

SRC='999'

where 999 can be any three digits.

I need a grep command that will return me the 999. How do I do this?

like image 955
Derek Avatar asked Nov 06 '09 19:11

Derek


2 Answers

Here is how to do it using sed

grep SRC=\'.*\' | sed 's/SRC=.\(.*\)./\1/'
like image 96
Alexander Egger Avatar answered Sep 30 '22 02:09

Alexander Egger


You can use the -o option on grep to return only the part of the string that matches the regex:

echo "SRC='999'" | grep -o -E '[0-9]{3}'
like image 38
Anton Geraschenko Avatar answered Sep 30 '22 03:09

Anton Geraschenko