我想在多个脚本中将端口号更改为 0,但我希望端口 0 之后的文本保持不变。有没有办法做到这一点。通过 vi,我可以更改模式,但不能更改端口号,因为它们都是唯一的。谢谢!
local-ip 159.105.100.40 port 5510 remote-ip 152.16.142.104 port 3868
答案1
惠斯sed
很简单:
$ foo="local-ip 159.105.100.40 port 5510 remote-ip 152.16.142.104 port 3868"
$ echo "$foo" | sed 's/port [0-9]\{1,5\}/port 0/g'
local-ip 159.105.100.40 port 0 remote-ip 152.16.142.104 port 0
所以
# let's suppose that all your scripts are in the same directory
# and have the extension .sh
for file in *.sh; do
# WARNING: the -i option writes the file
# so it's better to try first without it
sed -i 's/port [0-9]\{1,5\}/port 0/g' "$file"
done
您vi
可以使用相同的命令:
:s/port [0-9]\{1,5\}/port 0/g
或者更简单,如@卡西莫多建议:
:s/port \d\+/port 0/g