簡體   English   中英

使用grep,如何從文本斑點中提取每個數字?

[英]Using grep, how can I extract every number from a blob of text?

與之前的問題不同,我想在命令行上執行此操作(僅grep)。

我如何grep文本文件中的每個數字並顯示結果?

示范文本:

This is a sentence with 1 number, while the number 2 appears here, too.

我希望能夠從文本中提取“ 1”和“ 2”(當然,我的實際文本要長得多)。

我想你想要這樣的東西

$ echo 'This is a sentence with 1 number, while the number 2 appears here, too.' | grep -o '[0-9]\+'
1
2

由於基本sed使用BRE基本正則表達式 ),因此您需要轉義+符號,以便它將前一個字符重復一次或多次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM