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?
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?
Platform grep or general regular expression?
Regex
SRC\=\'(\d{3})\'
Are the lines to match always in the format SRC='nnn'
? Then you could use
grep SRC | cut -d"'" -f2
You can't do it with plain grep. As the man page on my box states: "grep - print lines matching a pattern" grep only prints lines, not part of lines.
I would recommend awk since it can do both the pattern matching and sub-line extracting:
awk -F\' ' /SRC/ {print $2}'
Here is how to do it using sed
grep SRC=\'.*\' | sed 's/SRC=.\(.*\)./\1/'
depends on your platform / language.
in Ruby:
string = "SRC = '999'"
string.match(/([0-9]{3})/).to_s.to_i
will return an integer
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}'