我正在尝试 grep 字符串和第一个 space 之间的值

I’m trying to grep value between string and the first space

我正在尝试 grep 字符串和第一个 space 之间的值。

我的文件包含以下行:

speed:10 temp_min:-14 temp_max:10 
speed:5 temp_min:-12 temp_max:10 

我想得到

grep "temp_min" file
-14
-12

非常感谢任何帮助。

使用grep -oP

grep -oP 'temp_min:\K\S+' file
-14
-12

或使用awk:

awk -F 'temp_min:' '{split(, a, " "); print a[1]}' file
-14
-12

或使用 `sed:

sed 's/.*temp_min:\([^[:blank:]]*\) .*//' file
-14
-12
grep -oP '(?<=temp_min:)[^ ]+' file.

如果你的 grep 支持 -P 你可以尝试 this.See 演示。

https://regex101.com/r/zM7yV5/14