提取子字符串

时间:2014-09-23 18:40:46

标签: shell substring

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

的字符串+ = NZ

请帮助!!!

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

如果您的字符串确实只包含country="CODE",那么cut也可以使用"作为分隔符:

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