2017-04-11 53 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] *通配符 –