gpt4 book ai didi

shell - BASH : Find number in text -> variable

转载 作者:行者123 更新时间:2023-12-04 23:09:12 30 4
gpt4 key购买 nike

我几乎不需要社区的帮助:

我在一个大文本文件中有这两行:

Connected clients: 42  
4 ACTIVE CLIENTS IN LAST 20 SECONDS

如何查找、提取数字并将其分配给变量?
clients=42
active=4

SED、AWK、GREP?我应该使用哪一种?

最佳答案

clients=$(grep -Po '^(?<=Connected clients: )([0-9]+)$' filename)
active=$(grep -Po '^([0-9]+)(?= ACTIVE CLIENTS IN LAST [0-9]+ SECONDS$)' filename)

或者
clients=$(sed -n 's/^Connected clients: \([0-9]\+\)$/\1/p' filename)
active=$(sed -n 's/^\([0-9]\+\) ACTIVE CLIENTS IN LAST [0-9]\+ SECONDS$/\1/p' filename)

关于shell - BASH : Find number in text -> variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4729683/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com