bash 从 sed 中提取多个捕获的组到变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13236566/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extract multiple captured groups from sed to variables
提问by Jawap
I have the following text
我有以下文字
abc <THIS> abc <THAT> abc <WHAT> abc
where abcis a placeholder for a well defined expression. I'd like to extract the 3 terms in the brackets and save them in 3 separate variables. Is is possible to do that without parsing the text 3 times? Basically I'd like to capture and somehow "export" multiple groups.
whereabc是定义明确的表达式的占位符。我想提取括号中的 3 个术语并将它们保存在 3 个单独的变量中。是否可以在不解析文本 3 次的情况下做到这一点?基本上我想捕获并以某种方式“导出”多个组。
It's clear that I can extract one of them like this:
很明显,我可以像这样提取其中之一:
VARIABLE=`echo $TEXT | sed "s_abc <\(.*\)> abc <.*> abc <.*> abc__g"`
But is it possible to get all 3 of them without running sed3 times?
但是是否有可能在不运行sed3 次的情况下获得所有 3个?
Other (portable) solutions without sedare also welcome.
sed也欢迎其他(便携式)解决方案。
回答by ruakh
If there are any characters that you know will notappear in THIS, THAT, or WHAT, then you can write something like this:
如果您知道有任何字符不会出现在THIS, THAT, 或 中WHAT,那么您可以这样写:
IFS=$'\t' read -r VAR1 VAR2 VAR3 \
    < <(sed 's/^abc <\(.*\)> abc <\(.*\)> abc <\(.*\)> abc$/\t\t/' \
             <<< "$TEXT"
       )
telling sedto use that separator in its output, and readto use that separator in its input.
告诉sed在其输出read中使用该分隔符,并在其输入中使用该分隔符。
回答by potong
This might work for you (GNU sed & bash):
这可能对您有用(GNU sed 和 bash):
line='abc <THIS> abc <THAT> abc <WHAT> abc'
var=($(sed 's/[^<]*<\([^>]*\)>[^<]*/"" /g' <<<"$line"))
echo "first ${var[0]} second ${var[1]} third ${var[2]}"
first "THIS" second "THAT" third "WHAT"
回答by jfg956
No need to spawn a process:
无需生成进程:
var='abc <THIS> abc <THAT> abc <WHAT> abc'
var1=${var#abc <}          # Remove the leading 'abc <'.
THIS="${var1%%> abc <*}"   # Remove the longest trailing '> abc <*'.
var2="${var1#*> abc <}"    # Remove the shortest leading '*> abc <'.
THAT="${var2%%> abc <*}"   # Remove the longest trailing '> abc <*'.
var3="${var2#*> abc <}"    # Remove the shortest leading '*> abc <'.
WHAT="${var3%> abc}"       # Remove the trailing '> abc'
echo "$THIS"
echo "$THAT"
echo "$WHAT"

