简体   繁体   中英

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.

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

With GNU grep:

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

\\K : removes matching part before \\K

Output:

22

I would use awk because it is portable in opposite to grep -o which only works with GNU grep:

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

awk splits the input into fields . The default field separator is a sequence of blank chars. NF is the number of fields , $NF is the last field.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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