2017-04-11 116 views
1

特定值i有我可以通过只在命令行键入使用grep显示在命令行

checklicence 

检查的产品的许可证。

它给了我输出:

Product License status 

Parameters of license Sxxxxx--xxxxx--xxxxx4 


Volume 
    Regular texts 
     Units: Pages 
     Quantity: 120000 per year 
     Remains: 107852 this year 

我想找到有多少留的是有和打印。

所以我试图做

>checklicense |grep -i "Remains:" 

我得到输出

>Remains: 107852 this year 

但我只是想作为输出数107852

有什么办法如何做到这一点的?

我的grep版本是License GPLv3+: GNU GPL version 3

+0

@Inian'许可证的GPLv3 +:GNU GPL版本3' – Shubham

+1

查找我的回答如下,让我知道是否能解决你的问题的 – Inian

+0

可能的复制(HTTP [正则表达式字符串之后和在太空中提取纯文本]:/ /stackoverflow.com/questions/18709439/regex-to-extract-only-text-after-string-and-before-space) – tripleee

回答

1

您可以用-o标志一起使用-P标志从GNU grep只匹配我们正在寻找的部分,

checklicence | grep -oP 'Remains:\s\K([^ ]\d+)' 
107852 

-P标志为GNU grep是为了启用Perl Compatible Regular Expressions 库。

\ K:This sequence resets the starting point of the reported match. Any previously matched characters are not included in the final matched sequence.

RegEx Demo


还值得补充的是,man grep页说

这是非常实验和grep -P可能会警告未执行的功能。

1
Checklicense |grep -i "Remains:" | grep -o "[0-9]*" 

这只能得到第一的grep的结果的数字部分

+0

grep -o做什么? – Shubham

+0

-o显示只匹配特定格式的部分,因为只需要数字[0-9] *通配符 –