Tuesday, July 26, 2022

[SOLVED] How to replace certain position after match using sed in Ubuntu?

Issue

I have a line I want to replace such as

'baseurl' => 'https://231.231.231.231'

But I only want it to replace the rel="nofollow noreferrer">https://231.231.231.231 part.

Basically I want it to be

'baseurl' => 'myvaluehere'

I have tried sudo sed -i "s|'baseurl'|${value1}|g" file.php

How do I replace after certain characters after 'baseurl' is matched?


Solution

Using sed

$ sed -E "/(baseurl'[^']*')[^']*/s//\1myvaluehere/" input_file
'baseurl' => 'myvaluehere'


Answered By - HatLess
Answer Checked By - Pedro (WPSolving Volunteer)