我被困在试图给grep什么刚过`名=` [英] I’m stuck in trying to grep anything just after `name=`

查看:135
本文介绍了我被困在试图给grep什么刚过`名=`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在试图用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 =

grep 'name=.*' filename

任何帮助很多AP preciated。

Any help is much appreciated.

推荐答案

由于详细的这里 ,你要积极的后向条款,如:

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.

使用正后向子句的优点是,名称=文本不匹配的部分。如果grep的亮点匹配的文本,它只会凸显字母数字(和空格)的一部分。 -o参数也将不显示NAME =的一部分。而且,如果你过渡这像sed另一个程序可能会捕获文本,并用它做什么,你不会捕捉NAME =的一部分,虽然你也可以做到这一点通过捕捉parenthess。

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什么刚过`名=`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆