增加参数文件中用点分隔的数字

Increase number delimited with dots from params file

我的 params.txt

中几乎没有参数
config_version = "1.2.3" 

test_version = "1.2.3" 

version_count = "1.2.3" 

service_name = "test"

already have answer from this forum how to get this number as output:

awk 'BEGIN{FS="[ \"]+"}/config_version =/{print }' /dir/params.txt

我需要为每个 awk 一个衬里 运行 增加 config_version 值 +1 的数量。示例:

first value is 1.2.3, after awk command run --> 1.2.4 and save back to file

first value is 1.200.1, after awk command run --> 1.200.2 and save back to file

first values is 8.999.99, after awk command run --> 9.000.0 and save back to file

在 awk 命令的每个 运行 之后,params.txt config_version 将更新为增加的值

params.txt 执行 5 次 awk 脚本后:

config_version = "1.2.8" 

test_version = "1.2.3" 

version_count = "1.2.3" 

service_name = "test"

在 Gitlab CI 上全部 运行 并且只有一行是我可以使用的选项。

"Gitlab job run (1) -> take 1.200.0 -> change to 1.200.1"

"Gitlab job run (2) -> take 1.200.1 -> change to 1.200.2"

"Gitlab job run (10) -> take 1.200.9 -> change to 1.201.0"

"Gitlab job run (xx) -> take 1.999.9 -> change to 2.000.0"

命令将 运行 作为 gitlab ci

中的一行
$ cat version.txt
1.2.3
$ awk -F. 'BEGIN{ OFS="." }{ ++; if (>9){ ++; =0 }; print ,, }' version.txt
1.2.4

现在我应该解释一下这段代码,但是解释起来太简单了。

编辑:

  • 这会查找“config_version”
  • 这使用 gawk
  • 这是单线
awk 'BEGIN{ OFS="." }/config_version/{match([=11=],/([0-9]+)+.([0-9]+)+.([0-9])+/,a); a[3]++; if(a[3]>9) { a[3]=0; a[2]++ }; if(a[2]>999){ a[2]=0; a[1]++ }; print "config_version = \"" a[1],a[2],a[3]"\""}!/config_version/'  version.txt > version.tmp; cp version.tmp version.txt

或(只是为了更好的可读性)

awk 'BEGIN{ OFS="." }
     /config_version/{match([=12=],/([0-9]+)+.([0-9]+)+.([0-9])+/,a);
       a[3]++; 
       if(a[3]>9) { a[3]=0; a[2]++ }; 
       if(a[2]>999){ a[2]=0; a[1]++ }; 
       print "config_version = \"" a[1],a[2],a[3]"\""}
     !/config_version/'  version.txt > version.tmp; cp version.tmp version.txt
  • 行匹配config_version将被匹配,并递增
  • 行匹配 config_version 将被复制而不改变
  • 输出发送到 version.tmp。如果您使用 version.txt 作为输出,您将得到一个零字节长度的文件..

考虑这个输入文件:

cat file

config_version = "1.999.9"
config_version = "1.200.9"
config_version = "1.000.0"
config_version = "9.999.9"

您可以试试这个 awk 来增加版本号:

awk '{gsub(/"/, "", $NF); split($NF, a, "."); $NF=""; n = a[1] a[2] "." a[3]; s = sprintf("%.1f", n + .1); p = length(s)-5; print [=11=] "\"" substr(s, 1, p) "." substr(s, p+1) "\""}' file

config_version = "2.000.0"
config_version = "1.201.0"
config_version = "1.000.1"
config_version = "10.000.0"

awk 的扩展形式:

awk '{
   gsub(/"/, "", $NF)
   split($NF, a, ".")
   $NF = ""
   n = a[1] a[2] "." a[3]
   s = sprintf("%.1f", n + .1)
   p = length(s) - 5
   print [=12=] "\"" substr(s, 1, p) "." substr(s, p+1) "\""
}' file
$ cat file
config_version = "1.9.8"
config_version = "1.9.89"
config_version = "1.9.9"
config_version = "1.9.98"
config_version = "1.98.9"
config_version = "1.99.9"
config_version = "1.998.9"
config_version = "9.999.9"
config_version = "9.99.98.999.9"
config_version = "9.99.99.999.9"

$ cat tst.awk
BEGIN { FS=OFS="\"" }
{
    orig = [=11=]
    newNum = oldStr = 
    gsub(/\./,"",newNum)
    newNum++
    newStr = ""
    while ( match(oldStr,/[^.]+$/) ) {
        digsBeg = length(newNum) - RLENGTH + 1
        newStr  = substr(newNum,digsBeg) (newStr == "" ? "" : "." newStr)
        oldStr  = substr(oldStr,1,RSTART-2)
        newNum  = substr(newNum,1,digsBeg - 1)
    }
     = newNum newStr
    print orig "\t->\t" [=11=]
}

$ awk -f tst.awk file
config_version = "1.9.8"        ->      config_version = "1.9.9"
config_version = "1.9.89"       ->      config_version = "1.9.90"
config_version = "1.9.9"        ->      config_version = "2.0.0"
config_version = "1.9.98"       ->      config_version = "1.9.99"
config_version = "1.98.9"       ->      config_version = "1.99.0"
config_version = "1.99.9"       ->      config_version = "2.00.0"
config_version = "1.998.9"      ->      config_version = "1.999.0"
config_version = "9.999.9"      ->      config_version = "10.000.0"
config_version = "9.9.98.999.9" ->      config_version = "9.9.99.000.0"
config_version = "9.9.99.999.9" ->      config_version = "10.0.00.000.0"

另一个awk 使用@EdMorton 的输入文件

$ awk 'function join(a,k,s,sep) {for(k in a) {s=s sep a[k]; sep="."}
                                 return s}
       BEGIN {FS=OFS="\""}
       /^config_version / {for(i=split(,v,"."); i; i--)
                               {if(length(v[i]+1) > length(v[i]) && i>1) gsub(/./,0,v[i]); 
                                else {v[i]++; break}}
                           =join(v)}1' file


config_version = "1.9.9"
config_version = "1.9.90"
config_version = "2.0.0"
config_version = "1.9.99"
config_version = "1.99.0"
config_version = "2.00.0"
config_version = "1.999.0"
config_version = "10.000.0"
config_version = "9.99.99.000.0"
config_version = "10.00.00.000.0"

将数字拆分成组件并实现一个全加器,最后将数字返回并打印。