如何从字符串中提取信息

时间:2010-04-24 23:38:32

标签: regex bash string string-parsing

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

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

每个号码的长度可以有所不同,但总体结构将保持不变。我想要做的是能够得到这些数字并将它们加载到一些bash变量中,即:

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

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

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

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

5 个答案:

答案 0 :(得分:4)

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

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

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

答案 1 :(得分:3)

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

read TMP1 RECORDS TMP2 DELETED TMP3 SKIPPED TMP4 WARNING

更新:您还可以使用set

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

答案 2 :(得分:1)

这是一个可以使用的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

答案 3 :(得分:0)

您可以尝试使用'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

答案 4 :(得分: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"

输出

$ ./shell.sh
Records: 2910
Deleted: 0
Skipped: 0
Warnings: 0