gpt4 book ai didi

linux - 文件操作 : shell script find and increment

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:02:22 26 4
gpt4 key购买 nike

 #something here
port 4444

#something here

port 4444

所以我有像上面这样的文件,我会找到带有单词“port”的行并将它旁边的数字(数字可以是任何数字 - 不一定固定为 4444)递增 1。

例如,结果文件应该是这样的形式

 #something here
port 4444

#something here

port 4445

任何想法,我认为我们需要使用 sed ,但不确定是否递增它。

编辑:再次抱歉,我以为我在 sed 中得到了答案,所以没有考虑职位。这是确切的文件内容

$cat file
data dev-1
type client
option type tcp
option host 99.160.131.163
option nodelay on
option port 6996
end

data dev-2
type client
option type tcp
option host 99.160.131.164
option nodelay on
option port 6996
end

我想更改端口号并增加它。再次抱歉给您带来的不便。

最佳答案

$ awk 'f && $2=="port"{ $NF=num} $2=="port"{ num=$NF+1; f=1 } 1 ' file
data dev-1
type client
option type tcp
option host 99.160.131.163
option nodelay on
option port 6996
end

data dev-2
type client
option type tcp
option host 99.160.131.164
option nodelay on
option port 6997
end

关于linux - 文件操作 : shell script find and increment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2221298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com