Linux sed 提取数字组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9260625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sed extracting group of digits
提问by Usman
I have tried to extract a number as given below but nothing is printed on screen:
我试图提取下面给出的数字,但屏幕上没有打印任何内容:
echo "This is an example: 65 apples" | sed -n 's/.*\([0-9]*\) apples//p'
However, I get '65', if both digits are matched separately as given below:
但是,如果两个数字分别匹配,我得到“65”,如下所示:
echo "This is an example: 65 apples" | sed -n 's/.*\([0-9][0-9]\) apples//p'
65
How can I match a number such that I don't know the number of digits in a number to be extracted e.g. it can be 2344 in place of 65?
如何匹配一个数字,以至于我不知道要提取的数字的位数,例如它可以是 2344 代替 65?
采纳答案by codaddict
$ echo "This is an example: 65 apples" | sed -r 's/^[^0-9]*([0-9]+).*//'
65
回答by FatalError
What you are seeing is the greedy behavior of regex. In your first example, .*
gobbles up all the digits. Something like this does it:
您所看到的是正则表达式的贪婪行为。在您的第一个示例中,.*
吞噬了所有数字。像这样的事情:
echo "This is an example: 65144 apples" | sed -n 's/[^0-9]*\([0-9]\+\) apples//p'
65144
This way, you can't match any digits in the first bit. Some regex dialects have a way to ask for non-greedy matching, but I don't believe sed
has one.
这样,您就无法匹配第一位中的任何数字。一些正则表达式方言有一种方法可以要求非贪婪匹配,但我认为没有sed
。
回答by mathematical.coffee
It's because your first .*
is greedy, and your [0-9]*
allows 0 or more digits.
Hence the .*
gobbles up as much as it can (including the digits) and the [0-9]*
matches nothing.
这是因为您的第一个.*
是greedy,并且您[0-9]*
允许 0 个或多个数字。因此.*
,尽可能多地吞噬(包括数字)并且不[0-9]*
匹配。
You can do:
你可以做:
echo "This is an example: 65 apples" | sed -n 's/.*\b\([0-9]\+\) apples//p'
where I forced the [0-9]
to match at least one digit, and also added a word boundary before the digits so the whole number is matched.
我强制[0-9]
匹配至少一位数字,并在数字前添加了一个单词边界,以便匹配整个数字。
However, it's easier to use grep
, where you match just the number:
但是,它更易于使用grep
,您只需匹配数字:
echo "This is an example: 65 apples" | grep -P -o '[0-9]+(?= +apples)'
The -P
means "perl regex" (so I don't have to worry about escaping the '+').
的-P
意思是“Perl的正则表达式”(所以我没有约逃避“+”的担心)。
The -o
means "only print the matches".
的-o
意思是“只打印匹配”。
The (?= +apples)
means match the digits followed by the word apples.
该(?= +apples)
方法相匹配的数字,后跟字苹果。
回答by ctrl-alt-delor
echo "This is an example: 65 apples" | ssed -nR -e 's/.*?\b([0-9]*) apples//p'
You will however need super-sed for this to work. The -R allows perl regexp.
但是,您需要使用 super-sed 才能使其正常工作。-R 允许 perl regexp。
回答by Khate
A simple way for extracting all numbers from a string
从字符串中提取所有数字的简单方法
echo "1213 test 456 test 789" | grep -P -o "\d+"
And the result:
结果:
1213
456
789