如何从字符串中提取信息

发布于 2024-08-30 07:42:13 字数 423 浏览 8 评论 0原文

我正在尝试从使用 bash 运行的负载中获取动态。我已经得到了我想要的字符串,现在我想从中提取某些可能变化的信息。返回的字符串如下:

Records: 2910 Deleted: 0 Skipped: 0 Warnings: 0

每个数字的长度可以并且将会变化,但整体结构将保持不变。我想要做的是能够获取这些数字并将它们加载到一些 bash 变量中,即:

RECORDS=??
DELETED=??
SKIPPED=??
WARNING=??

在正则表达式中我会这样做:

Records: (\d*?) Deleted: (\d*?) Skipped (\d*?) Warnings (\d*?)

并在我的变量中使用 4 个组。

I am trying to pull dynamics from a load that I run using bash. I have gotten to a point where I get the string I want, now from this I want to pull certain information that can vary. The string that gets returned is as follows:

Records: 2910 Deleted: 0 Skipped: 0 Warnings: 0

Each of the number can and will vary in length, but the overall structure will remain the same. What I want to do is be able to get these numbers and load them into some bash variables ie:

RECORDS=??
DELETED=??
SKIPPED=??
WARNING=??

In regex I would do it like this:

Records: (\d*?) Deleted: (\d*?) Skipped (\d*?) Warnings (\d*?)

and use the 4 groups in my variables.

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

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

发布评论

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

评论(5

帥小哥 2024-09-06 07:42:13

您可以在 Bash 版本 >= 3.2 中使用正则表达式匹配:

[[ $line =~ ([[:digit:]]+).*([[:digit:]]+).*([[:digit:]]+).*([[:digit:]]+) ]]

RECORDS=${BASH_REMATCH[1]}
DELETED=${BASH_REMATCH[2]}
SKIPPED=${BASH_REMATCH[3]}
WARNING=${BASH_REMATCH[4]}

You can use regex matching in Bash versions >= 3.2:

[[ $line =~ ([[:digit:]]+).*([[:digit:]]+).*([[:digit:]]+).*([[:digit:]]+) ]]

RECORDS=${BASH_REMATCH[1]}
DELETED=${BASH_REMATCH[2]}
SKIPPED=${BASH_REMATCH[3]}
WARNING=${BASH_REMATCH[4]}
掩耳倾听 2024-09-06 07:42:13

内置的read命令可以解决这个问题:

read TMP1 RECORDS TMP2 DELETED TMP3 SKIPPED TMP4 WARNING

更新:您还可以使用set

set $line
RECORDS=$2
DELETED=$4
SKIPPED=$6
WARNING=$8

Built-in read command will do the trick:

read TMP1 RECORDS TMP2 DELETED TMP3 SKIPPED TMP4 WARNING

Update: You can also use set:

set $line
RECORDS=$2
DELETED=$4
SKIPPED=$6
WARNING=$8
南街女流氓 2024-09-06 07:42:13

您可以使用以下 shell 函数来创建多个名称/值对。它假设内容的格式如您所说,但很容易更改:

parseline() {
    while [ $# -ge 2 ] ; do
        eval $(echo $1 | tr -d : | tr '[a-z]' '[A-Z]')="$2"
        shift 2
    done
}

像这样执行:

$ parseline Records: 2910 Deleted: 0 Skipped: 0 Warnings: 0
$ echo $RECORDS
2910
$ echo $WARNINGS
0

Here's a shell function you can use that just creates a number of name/value pairs. It assumes things are formatted as you said, but is easy to change:

parseline() {
    while [ $# -ge 2 ] ; do
        eval $(echo $1 | tr -d : | tr '[a-z]' '[A-Z]')="$2"
        shift 2
    done
}

Execute it like:

$ parseline Records: 2910 Deleted: 0 Skipped: 0 Warnings: 0
$ echo $RECORDS
2910
$ echo $WARNINGS
0
·深蓝 2024-09-06 07:42:13

您可以根据行的结构尝试使用“sed”或“cut”,如下所示:

#!/bin/sh                                                                       

DYNAMIC="Records: 2910 Deleted: 1 Skipped: 2 Warnings: 3"

RECORDS=`echo "$DYNAMIC" | sed 's/.*Records: \([0-9]*\).*/\1/g'`
DELETED=`echo "$DYNAMIC" | sed 's/.*Deleted: \([0-9]*\).*/\1/g'`
SKIPPED=`echo "$DYNAMIC" | sed 's/.*Skipped: \([0-9]*\).*/\1/g'`
WARNINGS=`echo "$DYNAMIC" | sed 's/.*Warnings: \([0-9]*\).*/\1/g'`

echo "Records $RECORDS"
echo "Deleted $DELETED"
echo "Skipped $SKIPPED"
echo "Warnings $WARNINGS"

echo "$DYNAMIC" | cut -d " " -f2
echo "$DYNAMIC" | cut -d " " -f4
echo "$DYNAMIC" | cut -d " " -f6
echo "$DYNAMIC" | cut -d " " -f8

You can try something with 'sed' or 'cut' depending on the structure of the line like this:

#!/bin/sh                                                                       

DYNAMIC="Records: 2910 Deleted: 1 Skipped: 2 Warnings: 3"

RECORDS=`echo "$DYNAMIC" | sed 's/.*Records: \([0-9]*\).*/\1/g'`
DELETED=`echo "$DYNAMIC" | sed 's/.*Deleted: \([0-9]*\).*/\1/g'`
SKIPPED=`echo "$DYNAMIC" | sed 's/.*Skipped: \([0-9]*\).*/\1/g'`
WARNINGS=`echo "$DYNAMIC" | sed 's/.*Warnings: \([0-9]*\).*/\1/g'`

echo "Records $RECORDS"
echo "Deleted $DELETED"
echo "Skipped $SKIPPED"
echo "Warnings $WARNINGS"

echo "$DYNAMIC" | cut -d " " -f2
echo "$DYNAMIC" | cut -d " " -f4
echo "$DYNAMIC" | cut -d " " -f6
echo "$DYNAMIC" | cut -d " " -f8
╰ゝ天使的微笑 2024-09-06 07:42:13
#!/bin/bash
s="Records: 2910 Deleted: 0 Skipped: 0 Warnings: 0"
s=${s//: /=}
for i in $(printf ${s// /"\n"})
do
 eval $i
done

echo "Records: $Records"
echo "Deleted: $Deleted"
echo "Skipped: $Skipped"
echo "Warnings: $Warnings"

输出

$ ./shell.sh
Records: 2910
Deleted: 0
Skipped: 0
Warnings: 0
#!/bin/bash
s="Records: 2910 Deleted: 0 Skipped: 0 Warnings: 0"
s=${s//: /=}
for i in $(printf ${s// /"\n"})
do
 eval $i
done

echo "Records: $Records"
echo "Deleted: $Deleted"
echo "Skipped: $Skipped"
echo "Warnings: $Warnings"

output

$ ./shell.sh
Records: 2910
Deleted: 0
Skipped: 0
Warnings: 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文