Linux 如何让 GREP 只选择数值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8402919/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:31:48  来源:igfitidea点击:

How to make GREP select only numeric values?

linuxbashgrep

提问by user710818

I use the dfcommand in a bash script:

df在 bash 脚本中使用命令:

df . -B MB | tail -1 | awk {'print '} | grep  .[0-9]*

This script returns:

此脚本返回:

99%

But I need only numbers (to make the next comparison). If I use the grepregex without the dot:

但我只需要数字(进行下一次比较)。如果我使用grep不带点的正则表达式:

df . -B MB | tail -1 | awk {'print '} | grep  .[0-9]*

I receive nothing. How to fix?

我什么也没有收到。怎么修?

采纳答案by Kent

If you try:

如果你试试:

 echo "99%" |grep -o '[0-9]*'

It returns:

它返回:

99


Here's the details on the -o(or --only-matchingflag) works from the grep manual page.

以下是grep 手册页中有关-o(或--only-matching标志)作品的详细信息。

Print only the matched (non-empty) parts of matching lines, with each such part on a separate output line. Output lines use the same delimiters as input, and delimiters are null bytes if -z (--null-data) is also used (see Other Options).

只打印匹配行的匹配(非空)部分,每个这样的部分在一个单独的输出行上。输出行使用与输入相同的分隔符,如果还使用了 -z (--null-data),则分隔符为空字节(请参阅其他选项)。

回答by Martin

grepwill print any linesmatching the pattern you provide. If you only want to print the part of the line that matches the pattern, you can pass the -ooption:

grep将打印与您提供的模式匹配的任何。如果只想打印与模式匹配的行部分,可以传递-o选项:

-o, --only-matchingPrint only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

-o, --only-matching只打印匹配行的匹配(非空)部分,每个这样的部分在一个单独的输出行上。

Like this:

像这样:

echo 'Here is a line mentioning 99% somewhere' | grep -o '[0-9]+'

回答by Alex Howansky

How about:

怎么样:

df . -B MB | tail -1 | awk {'print '} | cut -d'%' -f1

回答by anubhava

No need to used grep here, Try this:

不需要在这里使用 grep,试试这个:

df . -B MB | tail -1 | awk {'print substr(, 1, length()-1)'}

回答by mohdzamri

function getPercentUsed() {
    $sys = system("df -h /dev/sda6 --output=pcent | grep -o '[0-9]*'", $val);
    return $val[0];
}

回答by Donovan

Don't use more commands than necessary, leave away tail, grep and cut. You can do this with only (a simple) awk

不要使用不必要的命令,不要使用 tail、grep 和 cut。你可以只用(一个简单的)awk

PS: giving a block-size en print only de persentage is a bit silly ;-) So leave also away the "-B MB"

PS:只给出一个块大小的 en 打印有点傻 ;-) 所以也离开“-B MB”

df . |awk -F'[multiple field seperators]' '$NF=="Last field must be exactly --> mounted patition" {print $(NF-number from last field)}'

df 。|awk -F'[多个字段分隔符]' '$NF=="最后一个字段必须完全相同 --> 已安装的分区" {print $(NF- number from last field)}'

in your case, use:

在您的情况下,请使用:

df . |awk -F'[ %]' '$NF=="/" {print $(NF-2)}'

output:81

输出:81

If you want to show the percent symbol, you can leave the -F'[ %]' away and your print field will move 1 field further back

如果你想显示百分比符号,你可以离开 -F'[ %]' 并且你的打印字段将向后移动 1 个字段

df . |awk '$NF=="/" {print $(NF-1)}'

output:81%

产量:81%