简体   繁体   English

Sed 捕获组正则表达式

[英]Sed Capture Group Regex

I have the following string as output我有以下字符串为 output

Config(1) = ( value1:4000 value2:2000 value3:500 value4:1000)

I want to capture all 4 values into 4 different variables in bash and I think the cleanest way to do that is with regex.我想将所有 4 个值捕获到 bash 中的 4 个不同变量中,我认为最干净的方法是使用正则表达式。 And I think the best way to use regex for this is with sed.我认为为此使用正则表达式的最佳方法是使用 sed。 I have tested the regex and can capture the value1 with我已经测试了正则表达式,并且可以捕获 value1

value1:(\d+)

With sed I am trying this based on other answers:使用 sed 我正在尝试基于其他答案:

echo "Config(1) = ( value1:4000 value2:2000 value3:500 value4:1000)" | sed -n 's/^\s*value1\:\(\d\+\)\s\?.*/\1/p'

This returns nothing这不返回任何内容

BASH supports regular expressions natively: BASH 原生支持正则表达式:

#!/bin/bash

s='Config(1) = ( value1:4000 value2:2000 value3:500 value4:1000)'
pattern='value1:([0-9]+) value2:([0-9]+) value3:([0-9]+) value4:([0-9]+)'

if [[ "$s" =~ $pattern ]]
then
        echo "${BASH_REMATCH[1]}"
        echo "${BASH_REMATCH[2]}"
        echo "${BASH_REMATCH[3]}"
        echo "${BASH_REMATCH[4]}"
fi
4000
2000
500
1000

You could grep for the value with the -o flag to only output the match.您可以将带有-o标志的值 grep 仅匹配为 output 。

This outputs 4000这输出4000

echo "Config(1) = ( value1:4000 value2:2000 value3:500 value4:1000)" | grep -Po '(?<=value1:)\d+'

Though it's tough to advise about if its the cleanest way to achieve your goal without more context, a program (in awk maybe?) that parses that output format might be interesting here.尽管很难建议它是否是在没有更多上下文的情况下实现目标的最简洁方法,但解析 output 格式的程序(可能在awk中?)在这里可能很有趣。

This will work Create a simple two statement script这将起作用创建一个简单的两语句脚本

var=`echo "Config(1) = ( value1:4000 value2:2000 value3:500 value4:1000)" | grep -Eo "\( .*\)"|sed 's/^.\(.*\).$/\1/'`
for v in $var; do
 echo $v| awk -F: '{print $2}'
done

Run as运行方式

root@114855-T480:/home/yadav22ji# ./tpr
4000
2000
500
1000

You can assign these values to variables as you said.您可以按照您所说的将这些值分配给变量。

Could you please try following, it will match all string values in Input_file and will create an array out of its values.您能否尝试以下操作,它将匹配 Input_file 中的所有字符串values ,并将根据其值创建一个数组。

readarray -t arr < <\
(awk '
{
  while(match($0,/value[0-9]+:[0-9]+/)){
    val=substr($0,RSTART,RLENGTH)
    sub(/value[0-9]+:/,"",val)
    print val
    $0=substr($0,RSTART+RLENGTH)
  }
}' file
)

Above will create an array named arr you want to access its values then try:上面将创建一个名为arr的数组,您要访问它的值,然后尝试:

echo "${arr[@]}"

Parsing and capturing every value to the variable:解析和捕获变量的每个值:

result=`echo "Config(1) = ( value1:4000 value2:2000 value3:500 value4:1000)"`
declare -A variables=( ["variableone"]="1" ["variabletwo"]="2" ["variablethree"]="3" ["variablefour"]="4" )
for index in ${!variables[*]}
do
    export $index=$(echo $result | tr ' ' '\n' | sed "s/[()]//g" |  grep value | awk -F ":" '{print $2}' | head -"${variables[$index]}" | tail -1)
done

Array item- name of the variable数组项 - 变量的名称

Array index - counter line using in head command数组索引 - 在 head 命令中使用的计数器行


[root@centos ~]# env | grep variable
variablefour=1000
variableone=4000
variabletwo=2000
variablethree=500

I think following regex could be more shorter form in @hmm answer我认为在@hmm 答案中遵循正则表达式可能会更短

value[0-9]{1}:([0-9]+)值[0-9]{1}:([0-9]+)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM