This is my text:
set $domain_name some-not-important-name.com;
I want to reach this:
set $domain_name test.com;
This is what I tried so far:
DOMAIN_NAME=test.com
awk '{ sub(/ set $domain_name(.*)/, " set $domain_name $DOMAIN_NAME;") }1' file
current result:
set $domain_name some-not-important-name.com
Update 1
The reason I added multiple whitespaces
is that, the line belongs to nginx
config and it has multiple spaces.
Since there are both charset
and set
in my file and they’re not the first character of the line, so I should replace set
with space before.
2
Answers
UPDATE: (maintains spacing)
Setup:
One
awk
idea:Assumptions:
$3
does not match the stringsset
nor$domain_name
$3
does not contain white spaceThis generates:
Original answer: (does not maintain spacing)
Setup:
Assuming the line of interest has just the 3 space-delimited fields …
One
awk
idea:This generates:
This would work
you have to escape the
$