Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using sed with multiple conditions

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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/'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading