Here is the line of text:
这是文本行:
SRC='999'
where 999 can be any three digits.
其中999可以是任意三位数。
I need a grep command that will return me the 999. How do I do this?
我需要一个grep命令,它将返回给我999.我该怎么做?
3
Here is how to do it using sed
以下是使用sed的方法
grep SRC=\'.*\' | sed 's/SRC=.\(.*\)./\1/'
2
Are the lines to match always in the format SRC='nnn' ? Then you could use
SRC='nnn'
是否匹配的行始终采用SRC ='nnn'格式?然后你可以使用
grep SRC | cut -d"'" -f2
You can use the -o option on grep to return only the part of the string that matches the regex:
-o
grep
您可以在grep上使用-o选项仅返回与正则表达式匹配的字符串部分:
echo "SRC='999'" | grep -o -E '[0-9]{3}'
1
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.
你不能用普通的grep做到这一点。正如我的框上的手册页所述:“grep - 匹配模式的打印行”grep只打印行,而不是行的一部分。
I would recommend awk since it can do both the pattern matching and sub-line extracting:
我会推荐awk,因为它可以同时进行模式匹配和子行提取:
awk -F\' ' /SRC/ {print $2}'
just sed will do
只是sed会做的
$ echo SRC='999' | sed '/SRC/s/SRC=//' 999
0
Platform grep or general regular expression?
平台grep或一般正则表达式?
Regex
SRC\=\'(\d{3})\'
why grep? How about..
为什么grep?怎么样..
substr("SRC='999'",6,3)
depends on your platform / language.
取决于您的平台/语言。
in Ruby:
string = "SRC = '999'" string.match(/([0-9]{3})/).to_s.to_i
will return an integer
将返回一个整数