Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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

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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.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
Regex 用shell脚本中的新IP替换特定IP(精确匹配IP)地址_Regex_Shell_Sed - Fatal编程技术网

Regex 用shell脚本中的新IP替换特定IP(精确匹配IP)地址

Regex 用shell脚本中的新IP替换特定IP(精确匹配IP)地址,regex,shell,sed,Regex,Shell,Sed,我有一个包含许多id地址(IPV4和IPV6)的文件。我想使用sed命令将旧IP的所有事件替换为新IP。但我面临一个问题,例如: 像下面这样的老IP 2.2.2.2 -IPV4 2:2:2.2.2.2 - IPV6: Note ':' 在ksh中,我使用 oldIP=2.2.2.2 newIP=3.3.3.3 sed -i 's/'$oldIP'/'$newIP'/g' filename. 但这将同时替换2.2.2.2和2:2:2.2.2

我有一个包含许多id地址(IPV4和IPV6)的文件。我想使用sed命令将旧IP的所有事件替换为新IP。但我面临一个问题,例如:

像下面这样的老IP

2.2.2.2             -IPV4      
2:2:2.2.2.2         - IPV6: Note ':'
在ksh中,我使用

oldIP=2.2.2.2
newIP=3.3.3.3

sed -i 's/'$oldIP'/'$newIP'/g' filename.
但这将同时替换2.2.2.2和2:2:2.2.2.2,因为oldIP变量中的“.”用作正则表达式

有人能告诉我们如何在scipt中匹配文件的确切IP吗

输入文件:a.txt-包含旧IP、新IP

1.1.1.1,9.9.9.9
0.0.0.0,9.9.9.9
2.2.2.2,9.9.9.9
5:5:5.5.5.5,[9:9:9.9.9.9]
3.3.3.3,9.9.9.9
3:3:3.3.3.3,9:9:9.9.9.9
1:1:3.3.3.3,9:9:9.9.9.9

#!/bin/ksh

ipAddrFile=$1
while read line
do
    OLDIFS=$IFS
    IFS=","
    array=( $line )
    IFS=$OLDIFS

    if [ "${array[1]}" = "" ]; then
        echo "tokenize ip address list fail. Check if proper separator is used."
    fi

    oldIP=${array[0]}
    newIP=${array[1]}

    `sed -i 's/'$oldIP'/'$newIP'/g' temp.xml`
       if [ $? -ne 0 ]; then
       echo "Replacing field value failed"
      exit 1
   fi


done < $ipAddrFile
1.1.1,9.9.9.9
0.0.0.0,9.9.9.9
2.2.2.2,9.9.9.9
5:5:5.5.5.5,[9:9:9.9.9.9]
3.3.3.3,9.9.9.9
3:3:3.3.3.3,9:9:9.9.9.9
1:1:3.3.3.3,9:9:9.9.9.9
#!/bin/ksh
IPAddressFile=$1
读行时
做
OLDIFS=$IFS
IFS=“,”
数组=($line)
IFS=$OLDIFS
如果[“${array[1]}”=”;然后
echo“标记化ip地址列表失败。检查是否使用了正确的分隔符。”
fi
oldIP=${array[0]}
newIP=${array[1]}
`sed-i's/'$oldIP'/'$newIP'/g'temp.xml`
如果[$?-ne 0];然后
回显“替换字段值失败”
出口1
fi
完成<$IPAddressFile

首先,点需要在正则表达式中转义,否则它将匹配任何字符。因此,如下设置变量:

oldIP="2\.2\.2\.2"
newIP="3.3.3.3"
然后,您可以使用此sed:

sed -r "s/(^|[^:])$oldIP([^0-9]|$)/\1$newIP\2/g" input
或在Mac上:

sed -E "s/(^|[^:])$oldIP([^0-9]|$)/\1$newIP\2/g" input

这个命令的作用是什么?我在Linux上,第一个不工作。命令无法执行。它的错误。
这个命令的作用是什么?
这是对您的尝试的替换
sed-i的/'$oldIP'/'$newIP'/g'文件名
我想您不需要在替换变量中转义
。@devnull:是的,它不会改变行为,但我只是不想给未来的访问者错误的信息,这样会更好阿努巴瓦:这就是促使我首先评论的原因。很好地解决了这个问题+1.