简体   繁体   English

如何用 sed 替换版本号?

[英]How to replace a version number with sed?

  <package id="Antlr3.Runtime" version="3.5.1" targetFramework="net471" />
  <package id="MathNet.Numerics" version="4.12.0" targetFramework="net471" />
  <package id="NCalcAsync" version="3.1.8" targetFramework="net471" />
  <package id="Newtonsoft.Json" version="13.0.1" targetFramework="net471" />
  <package id="OdicoGeometryOps" version="1.0.0-20221118-143402" targetFramework="net471" />
  <package id="System.ValueTuple" version="4.5.0" targetFramework="net471" />
  <package id="VSDaVinci" version="1.0.0-20221118-143815" targetFramework="net471" />

I have a package file like this and I would like to update my version for line #5 and line #7 using sed. Another thing worth mentioning is that the version number for line #5 and line #7 is not static so at any given point it may change.我有一个这样的 package 文件,我想使用 sed 更新第 5 行和第 7 行的版本。另一件值得一提的是,第 5 行和第 7 行的版本号不是 static,所以在任何给定的情况下点它可能会改变。 How can I do this?我怎样才能做到这一点?

Edit: My req may not be very clear so I'm adding a couple more points.编辑:我的要求可能不是很清楚,所以我再补充几点。 All i want to do is to change the version number on line #5 and line #7.我只想更改第 5 行和第 7 行的版本号。 The version number that i will be changing to and the one im changing is dynamic and i will get it as an argument to a file which i will be using to update it.我将要更改的版本号和我正在更改的版本号是动态的,我将把它作为我将用来更新它的文件的参数。

#!/bin/bash
#file name
File=demo.txt

##get 2 arguments 
lb1=$1
lb2=$2

sed -i "/^[[:space:]]*[^#]*keyword/c replaceable line" $File
sed -i "/^[[:space:]]*[^#]*keyword/c replaceable line" $File

the above script searches for the keyword and replaces the entire line with the replaceable line Note: it also ignores commented entries in the text file上面的脚本搜索关键字并用可替换行替换整行注意:它还会忽略文本文件中的注释条目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM