Home > Software engineering >  Using sed with multiple conditions
Using sed with multiple conditions

Time:05-04

I have a .spec file that has a lines such as the following:

Requires: rpm_name           = 1.0.0
Requires: longer_rpm_name    = 1.0.1

I am putting together a script that will write to the .spec file and update the rpm versions with their respective newer versions.

Using sed I can do something like sed -i 's/Requires: rpm_name.*/Requires: rpm_name = 2.0.0'. This will result in the following:

Requires: rpm_name = 2.0.0
Requires: longer_rpm_name    = 1.0.1

Rather than replace the entire line I would like to simply replace the version number that follows the = character such that the formatting of the lines remains the same. I would like:

Requires: rpm_name          = 2.0.0
Requires: longer_rpm_name   = 1.0.1

I'd like sed to find the line starting with Requires: rpm_name and the only replace the version number instead of the entire line. Is this possible?

CodePudding user response:

Use an address specification at the beginning of the command to match the line. Then use a regexp in the s command to replace just the number at the end.

sed -i '/^Requires: rpm_name/s/[0-9.]*$/2.0.0/'

CodePudding user response:

Remember the part until the equal sign

sed -ri 's/(Requires: rpm_name.*)=/\1= 2.0.0/'

CodePudding user response:

Using sed

$ sed 's/\(\<rpm_name \ =\).*/\1 2.0.0/' input_file
Requires: rpm_name           = 2.0.0
Requires: longer_rpm_name    = 1.0.1
  • Related