UPDATE: (maintains spacing)
Setup:
$ cat file
ignore this line
set $domain_name some-not-important-name.com;
ignore this line
One awk
idea:
DOMAIN_NAME=test.com
awk -v dn="${DOMAIN_NAME}" ' # set awk variable "dn" to OS variable value
$1 == "set" && $2 == "$domain_name" { ndx = index($0,$3) # find location of 3rd field
$0 = substr($0,1,ndx-1) dn ";" # rebuild current line
}
1 # print all lines to stdout
' file
Assumptions:
- there are only 3 white-spaced delimited fields in the line of interest
$3
does not match the strings set
nor $domain_name
$3
does not contain white space
This generates:
ignore this line
set $domain_name test.com;
ignore this line
Original answer: (does not maintain spacing)
Setup:
$ cat file
ignore this line
set $domain_name some-not-important-name.com;
ignore this line
Assuming the line of interest has just the 3 space-delimited fields ...
One awk
idea:
DOMAIN_NAME=test.com
awk -v dn="${DOMAIN_NAME}" '
$1 == "set" && $2 == "$domain_name" { $3 = dn ";"} # redefine the 3rd field as dn + ";"
1 # print all lines to stdout
' file
# or as a one-liner:
awk -v dn="${DOMAIN_NAME}" '$1 == "set" && $2 == "$domain_name" { $3 = dn ";"} 1' file
This generates:
ignore this line
set $domain_name test.com;
ignore this line