繁体   English   中英

使用sed查找并替换一系列数字

[英]Using sed to find and replace a range of numbers

第一张海报在这里。

尝试使用sed查找和替换单词。 代码如下:

configFile=ConnectionConfig.xml
sed 's/30??\" authentication=\"someuniqueauthkey/'$1'\" authentication=\"someuniqueauthkey/' $configFile

我会像这样运行文件:

./choosePort.sh 3001

当我运行这段代码时,没有给出像正则表达式格式错误那样的错误,它什么也不会取代,并且tempXml.xml的内容只是ConnectionConfig的内容,没有发生变化。

我想做的是识别30中的任何数字3000-3099? 表达式的一部分,我以为是“?” 没有。

我要更改的输入行是:

<host id="0" address="someip" port="3001" authentication="someauthkey"

提前致谢。 (出于安全原因,文件中的ip和authkeys被清空了)。

[0-9]代替? 匹配一个数字。

sed 's/30[0-9][0-9]\" authentication=\"someuniqueauthkey/'$1'\" authentication=\"someuniqueauthkey/' $configFile

要匹配数字,您可以使用[0-9]\\{2}\\d\\{2}

你的情况?? 也只能匹配30

您可以在这里找到正则表达式的概述。

首先,匹配的表达式必须为30..而不是30??
然后,您似乎忘记了sed的-e参数。
试试:

sed -e 's/30..\" authentication=\"someuniqueauthkey/'$1'\" authentication=\"someuniqueauthkey/' $configFile

这可能对你有用:

sed 's/30[0-9][0-9]\(" authentication="someuniqueauthkey\)/'$1'\1/' $configFile

暂无
暂无

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

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