如何使用grep来提取子字符串? [英] How to use grep to extract a substring?

查看:400
本文介绍了如何使用grep来提取子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

从字符串中提取正则表达式结果并将其写入变量


这是我的命令:

lockquote
grep -E'\ * [[:它的输出结果如下:[]:+ FIN [[:space:]] +([^)] +?)')myfile


$ b



  • FIN(SUCCESS)

我只想输出:


SUCCESS


如何告诉 grep 来做到这一点?


<您可以将您的grep命令的输出传递给awk命令。

 <$> 

c $ c> grep -E'* [[:space:]] + FIN [[:space:]] +([^)] +?)')myfile | awk'{print $ 2}'

我不确定如何单独使用grep来做到这一点,因为它并不是真正适合这种确切的用例。因为你在一个grep平台上,所以当你有一个命令解决问题的一部分时,使用管道是你的优势,另一个命令则是另一个命令。


Possible Duplicate:
extract regexp result from string and write it to a variable

Here is my command :

grep -E '\*[[:space:]]+FIN[[:space:]]+([^)]+?)') myfile

It outputs :

  • FIN (SUCCESS)

And I would like it outputs only :

SUCCESS

How can I tell grep to do it ?

解决方案

You can pipe the output of your grep command to the awk command.

grep -E '*[[:space:]]+FIN[[:space:]]+([^)]+?)') myfile | awk '{print $2}'

I am not sure how to do that with grep alone, as it is not really tailored to that exact use case. Since you are on a platform where grep is, use pipes to your advantage when you can have one command solve part of the problem, and another command the other part.

这篇关于如何使用grep来提取子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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