Optimal solution to extract value from line using Grep
Could you please tell me what is the optimal way to extract idle 开发者_运维技巧value from this line using grep?
CPU states: 0.1% user, 0.1% system, 0.0% nice, 99.8% idle
awk
should do the trick:
top -n 1 | grep "idle" | awk '{ print $9 }'
Since the idle-percentage is the ninth value, it's $9
.
You can use grep
alone:
grep -Po '[0-9.%]+(?= idle)'
精彩评论