Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于shell脚本中的特殊字符从变量中提取子字符串_Shell_Substring - Fatal编程技术网

基于shell脚本中的特殊字符从变量中提取子字符串

基于shell脚本中的特殊字符从变量中提取子字符串,shell,substring,Shell,Substring,我有一个变量,我想从中提取由特殊字符包围的内容。在我的例子中,字符是% 变量中存储的值如下所示: “某物..:某物..%所需值1%某物..%所需值2%:某物..” 响应用不同的特殊字符填充,值来自不同的脚本 从上面的响应中,我想提取用%包围的值。我想要的输出是 所需值1 所需值2 使用awk: awk -v RS='%' '(NR+1)%2' <<< "something.. : something.. %Required Value 1% something.. %Requ

我有一个变量,我想从中提取由特殊字符包围的内容。在我的例子中,字符是
%

变量中存储的值如下所示:

“某物..:某物..%所需值1%某物..%所需值2%:某物..”
响应用不同的特殊字符填充,值来自不同的脚本

从上面的响应中,我想提取用
%
包围的值。我想要的输出是

所需值1
所需值2
使用awk:

awk -v RS='%' '(NR+1)%2' <<< "something.. : something.. %Required Value 1% something.. %Required Value 2% : something.. "
Required Value 1
Required Value 2
awk-vrs='%''(NR+1)%2'使用awk:

awk -v RS='%' '(NR+1)%2' <<< "something.. : something.. %Required Value 1% something.. %Required Value 2% : something.. "
Required Value 1
Required Value 2
awk-vrs='%''(NR+1)%2'使用Perl

> export X="something.. : something.. %Required Value 1% something.. %Required Value 2% : something.. "
> perl -e ' BEGIN { $p=$ENV{X}; while($p=~m/(.+?)%(.+?)%(.+)/) { $p=$3; print "$2\n" }} '
Required Value 1
Required Value 2
>
使用Perl

> export X="something.. : something.. %Required Value 1% something.. %Required Value 2% : something.. "
> perl -e ' BEGIN { $p=$ENV{X}; while($p=~m/(.+?)%(.+?)%(.+)/) { $p=$3; print "$2\n" }} '
Required Value 1
Required Value 2
>