gpt4 book ai didi

linux - Shell 脚本 - 读取属性文件和两个变量的加法(数学)

转载 作者:太空宇宙 更新时间:2023-11-04 10:05:22 26 4
gpt4 key购买 nike

我正在编写一个将在循环中运行的程序,我需要增加作为变量传入的时间毫秒数。它用于时间戳计算。

我发现了如何像这样更改属性文件中的属性:

sed -i "/exampleKey=/ s/=.*/=newExampleValue1/" test.properties

但在此之前我希望能够获取 currentExampleValue1 并对其执行加法..

像这样:

exampleKey=1000

//Get Current value here (1000)

sed -i "/exampleKey=/ s/=.*/= (current value + 500) /" test.properties

这样属性文件现在是:

exampleKey=1500

在 Linux 中有没有一种简单的方法可以做到这一点?我应该指出,我对 shell 脚本编写非常陌生。

最佳答案

sed 不能做数学运算。 Perl 可以:

perl -i~ -pe '/exampleKey=/ and s/=(.*)/"=" . ($1 + 500)/e' test.properties
  • -p逐行读取文件,处理后逐行打印

  • /e 将替换部分计算为代码。

您可以对较短的代码使用回顾断言:

s/(?<==)(.*)/$1 + 500/e

即将 = 前面的所有内容替换为自身 + 500。

关于linux - Shell 脚本 - 读取属性文件和两个变量的加法(数学),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52990875/

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