我是bash脚本的新手,我需要awk的帮助。所以事情是我有一个内部带有版本的属性文件,我想更新它。
version=1.1.1.0
我用awk来做
file="version.properties"
awk -F'["]' -v OFS='"' '/version=/{
split($4,a,".");
$4=a[1]"."a[2]"."a[3]"."a[4]+1
}
;1' $file > newFile && mv newFile $file
但我得到奇怪的结果version =“ 1.1.1.0”“ ... 1
有人可以帮我吗。
您在评论中提到您要就地更新文件。您可以使用perl在单一语言中做到这一点:
perl -pe '/^version=/ and s/(\d+\.\d+\.\d+\.)(\d+)/$1 . ($2+1)/e' -i version.properties
-e
然后是要运行的脚本。使用-p
和-i
,结果是在每一行上运行该脚本,并在脚本进行任何更改时在适当位置修改文件。
脚本本身(为便于说明)分为:
/^version=/ and # Do the following on lines starting with `version=`
s/ # Make a replacement on those lines
(\d+\.\d+\.\d+\.)(\d+)/ # Match x.y.z.w, and set $1 = `x.y.z.` and $2 = `w`
$1 . ($2+1)/ # Replace x.y.z.w with a copy of $1, followed by w+1
e # This tells Perl the replacement is Perl code rather
# than a text string.
$ cat foo.txt
version=1.1.1.2
$ perl -pe '/^version=/ and s/(\d+\.\d+\.\d+\.)(\d+)/$1 . ($2+1)/e' -i foo.txt
$ cat foo.txt
version=1.1.1.3
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句