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,我必须找到一个子字符串,其中字符串以country=“开头,以结尾,如下所示- 国家=“新西兰” 我必须只提取NZ部分并将其添加到现有字符串中,如- 字符串+=NZ 请帮忙 在正则表达式模式下使用sed: string="" INPUT='country="NZ"' string+=$(echo $INPUT | sed -r 's/country="(.*?)"/\1/') 如果字符串确实只包含country=“code”,则cut也可以使用“作为分隔符: echo 'country="NZ

我必须找到一个子字符串,其中字符串以country=“开头,以结尾,如下所示- 国家=“新西兰” 我必须只提取NZ部分并将其添加到现有字符串中,如-

字符串+=NZ


请帮忙

在正则表达式模式下使用
sed

string=""
INPUT='country="NZ"'
string+=$(echo $INPUT | sed -r 's/country="(.*?)"/\1/')

如果字符串确实只包含
country=“code”
,则
cut
也可以使用
作为分隔符:

echo 'country="NZ"' | cut -d\" -f2

哪个shell?bash、csh、dos?谢谢Nicolas,当字符串存储在名为POS.xml的文件中时,我们可以提取子字符串吗?实际上,有许多字符串包含格式为country=“NZ”的国家代码,我只需要代码部分。