Issue
I have a .spec
file that has 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 new 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 line remains the same. I would like:
Requires: rpm_name = 2.0.0
Requires: longer_rpm_name = 1.0.1
I would like sed
to find the line starting with Requires: rpm_name
and then 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/'
Answered By - Barmar Answer Checked By - Dawn Plyler (WPSolving Volunteer)