简体   繁体   English

grep命令尝试获取数值

[英]grep command trying to fetch numeric value

From below text in file: 在文件下方的文本中:

current_build: 22
previous_build: 55

I am only trying to grep number value of current_build. 我只是想grep当前值的数字值。

When run following command 当运行以下命令时

grep -o -E '[0-9]+' textfile

The output is both number like below 输出是两个数字,如下所示

22    
55

How do i only grep the value 22 我如何只grep值22

With GNU grep: 使用GNU grep:

grep -Po 'current_build: \K.*' file

\\K : removes matching part before \\K \\K :删除\\ K之前的匹配部分

Output: 输出:

22

I would use awk because it is portable in opposite to grep -o which only works with GNU grep: 我将使用awk因为它与grep -o相反,它可移植,仅与GNU grep一起使用:

awk '/current_build/{print $NF}' file

awk splits the input into fields . awk将输入分成多个字段 The default field separator is a sequence of blank chars. 默认的字段分隔符是一系列空白字符。 NF is the number of fields , $NF is the last field. NF字段数$NF是最后一个字段。

仅在行首打印与current_build匹配的行,并在最后一个空格之后显示部分:

sed -n 's/current_build:.* //p' file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM