Using perl to replace file content
In the build.gradle.kts has a versionNumber variable, the content is like below:
In the build.gradle.kts
has a versionNumber
variable, the content is like below:versionName = "1.0"
And I want to replace it with a build number on the bash script, the build number will increase every time when the bash script executes, the new versionNumber
should like below:versionName = "1.0.test.1"
The test
is the test environment, and the last 1
is the build number, we can use the perl
to update the file content like below:
The content of the build.gradle.kts
versionName = "1.0"
The replace command:perl -pi -e 's/versionName = \"(.*?)\"/versionName = \"$1.test.1\"/g' build.gradle.kts
After execute the command above, the content of the build.gradle.kts
will like below:versionName = "1.0.test.1"
Replace content in bash script#!/bin/bashbuildNumber=1
env=testperl -pi -e "s/versionName = \"(.*?)\"/versionName = \"\$1.$env.$buildNumber\"/g" build.gradle.kts
Because the pattern should has $1
, so on the bash script, we need escape the $
sign before $1
.