我有一个文件包含这个字符串requires="ua-common/2.7.25@aep/stable"
(版本号是可变的(,我需要用requires="ua-common/2.7.26@aep/stable"
(版本号也是可变的(替换它。所以我需要在不知道它的值的情况下更改版本号。我需要做一个这样的小脚本:
#!/bin/bash
echo "Insert version number: "
read version
#replace $version with old version in file
谢谢
我就是这样理解你的问题的。
文件file.txt
fjlakjflajkflkajfjakjfalkjfoairujnasncv
O
aljflajflja ljfaljflakjflakjf
ia;jflajfjaljfajflajfoiuqoruaf
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
requires="ua-common/2.7.25@aep-stable"
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
jalfjalkfjaoeiurjasnvafnaojf
jvjvg]
iajfiaufurva ajfaj
脚本myscript
#!/usr/bin/env bash
current_version=$(grep -oP 'requires="ua-common/K.*?(?=@)' file.txt)
printf 'The current version is: %snn' "$current_version"
read -rp "Insert a new version number: " version
if [[ -n $version ]]; then
sed "s|(requires=.*ua-common/).*(@.*)$|1$version2|" file.txt
fi
然后运行
bash ./myscript
输出
The current version is: 2.7.25
Insert a new version number:
输入新版本号:
The current version is: 2.7.25
Insert a new version number: 2.7.26
输出
fjlakjflajkflkajfjakjfalkjfoairujnasncv
O
aljflajflja ljfaljflakjflakjf
ia;jflajfjaljfajflajfoiuqoruaf
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
requires="ua-common/2.7.26@aep-stable"
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
jalfjalkfjaoeiurjasnvafnaojf
jvjvg]
iajfiaufurva ajfaj
当前脚本还有一些事情要做,比如错误检查、出错时退出等等,但这只是一个开始。
第一个解决方案:
使用您显示的示例(这将适用于以示例格式显示的任何版本(,请尝试以下awk
代码。这将给出具有所示样本的ua-common/2.7.26@aep/stable
输出。
echo "$requires" |
awk '
match($0,/([0-9]+.)*[0-9]+/){
value=substr($0,RSTART,RLENGTH)
num=split(value,arr,".")
arr[num]+=1
value=""
for(i=1;i<=num;i++){
value=(value?value ".":"")arr[i]
}
print substr($0,1,RSTART-1) value substr($0,RSTART+RLENGTH)
}
'
第二个解决方案:如果您的输入始终相同,并且您想在@
之前增加数字,请尝试以下操作。
echo "$requires" |
awk '
match($0,/[0-9]+@/){
print substr($0,1,RSTART-1) substr($0,RSTART+1,RLENGTH-1)+1 substr($0,RSTART+RLENGTH-1)
}
'
自从您请求bash以来最明显的解决方案(不适用于主要链接到/bin/sh的dash(
#!/bin/bash
oldVersion=2.7.25
newVersion=2.7.26
requires="ua-common/2.7.25@aep/stable"
echo "${requires//$oldVersion/$newVersion}"
输出:
ua-common/2.7.26@aep/stable
参考页:https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
您可能需要将sed用于该作业。根据需要调整sed搜索模式的文件内容:
#: cat test.file
requires="ua-common/2.7.25@aep/stable"
#: sed -i 's/^requires="ua-common/[0-9]*.[0-9]*.[0-9]*.@/requires="ua-common/1.2.3@/g' test.file
#: cat test.file
requires="ua-common/1.2.3@aep/stable"