BASH:在文本中查找数字 ->多变的

发布于 2024-10-12 19:56:49 字数 236 浏览 2 评论 0原文

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

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

Connected clients: 42  
4 ACTIVE CLIENTS IN LAST 20 SECONDS  

如何查找、提取数字并将其分配给变量?

clients=42
active=4

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

I need little help from the community:

I have these two lines in a large text file:

Connected clients: 42  
4 ACTIVE CLIENTS IN LAST 20 SECONDS  

How I can find, extract and assign the numbers to variables?

clients=42
active=4

SED, AWK, GREP? Which one should I use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖树树初阳… 2024-10-19 19:56:49
clients=$(grep -Po '^(?<=Connected clients: )([0-9]+)

或者

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)
filename) active=$(grep -Po '^([0-9]+)(?= ACTIVE CLIENTS IN LAST [0-9]+ SECONDS$)' filename)

或者

clients=$(grep -Po '^(?<=Connected clients: )([0-9]+)

or

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)
filename) active=$(grep -Po '^([0-9]+)(?= ACTIVE CLIENTS IN LAST [0-9]+ SECONDS$)' filename)

or

我很OK 2024-10-19 19:56:49
str='Connected clients: 42 4 ACTIVE CLIENTS IN LAST 20 SECONDS'

set -- $str
clients=$3
active=$4

如果是两条线就好了。

str1='Connected clients: 42'
str2='4 ACTIVE CLIENTS IN LAST 20 SECONDS'

set -- $str1
clients=$3
set -- $str2
active=$1

从文件中读取两行可以通过以下方式完成

{ read str1; read str2; } < file

:交替地在 AWK 中进行读取和写入,然后将结果放入 Bash 中。

eval "$(awk '/^Connected clients: / { print "clients=" $3 }
             /[0-9]+ ACTIVE CLIENTS/ { print "active=" $1 }
            ' filename)"
str='Connected clients: 42 4 ACTIVE CLIENTS IN LAST 20 SECONDS'

set -- $str
clients=$3
active=$4

If it's two lines, fine.

str1='Connected clients: 42'
str2='4 ACTIVE CLIENTS IN LAST 20 SECONDS'

set -- $str1
clients=$3
set -- $str2
active=$1

Reading two lines from a file may be done by

{ read str1; read str2; } < file

Alternately, do the reading and writing in AWK, and slurp the results into Bash.

eval "$(awk '/^Connected clients: / { print "clients=" $3 }
             /[0-9]+ ACTIVE CLIENTS/ { print "active=" $1 }
            ' filename)"
哭了丶谁疼 2024-10-19 19:56:49

可以

$ set -- $(awk '/Connected/{c=$NF}/ACTIVE/{a=$1}END{print c,a}' file)
$ echo $1
42
$ echo $2
4

根据需要使用 awk 将 $1, $2 分配给适当的变量名称

如果可以使用声明直接分配,则

$ declare $(awk '/Connected/{c=$NF}/ACTIVE/{a=$1}END{print "client="c;print "active="a}' file)
$ echo $client
42
$ echo $active
4

you can use awk

$ set -- $(awk '/Connected/{c=$NF}/ACTIVE/{a=$1}END{print c,a}' file)
$ echo $1
42
$ echo $2
4

assign $1, $2 to appropriate variable names as desired

if you can directly assign using declare

$ declare $(awk '/Connected/{c=$NF}/ACTIVE/{a=$1}END{print "client="c;print "active="a}' file)
$ echo $client
42
$ echo $active
4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文