如何使用grep在`name =`之后获取任何内容? [英] How to use grep to get anything just after `name=`?
问题描述
我一直在尝试 grep name=
之后的任何内容,只包含空格和字母数字.
I’m stuck in trying to grep anything just after name=
, include only spaces and alphanumeric.
例如:
name=some value here
我明白
some value here
我完全是新手,下面的 grep 匹配包括 name=
在内的所有内容.
I’m totally newb in this, the following grep match everything including the name=
.
grep 'name=.*' filename
非常感谢任何帮助.
推荐答案
详细这里,你想要一个肯定的lookbehind子句,比如:
As detailed here, you want a positive lookbehind clause, such as:
grep -P '(?<=name=)[ A-Za-z0-9]*' filename
-P 使 grep 使用 Perl 方言,否则您可能需要转义括号.您还可以如其他地方所述,附加 -o
参数以仅打印出匹配的内容.括号中的部分指定您需要字母数字和空格.
The -P makes grep use the Perl dialect, otherwise you'd probably need to escape the parentheses. You can also, as noted elsewhere, append the -o
parameter to print out only what is matched. The part in brackets specifies that you want alphanumerics and spaces.
使用肯定的lookbehind 子句的优点是name="文本不是匹配的一部分.如果 grep 突出显示匹配的文本,它只会突出显示字母数字(和空格)部分.-o 参数也不会显示name="部分.而且,如果您将其转换为另一个程序,例如 sed,它可能会捕获文本并对其进行处理,您将不会捕获name="部分,尽管您也可以通过使用捕获括号来实现这一点.
The advantage of using a positive lookbehind clause is that the "name=" text is not part of the match. If grep highlights matched text, it will only highlight the alphanumeric (and spaces) part. The -o parameter will also not display the "name=" part. And, if you transition this to another program like sed that might capture the text and do something with it, you won't be capturing the "name=" part, although you can also do that by using capturing parenthess.
这篇关于如何使用grep在`name =`之后获取任何内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!