Reputation: 6333
I'm trying to use the following command:
awk '/dev-api2\.company-private/{p=NR} p && NR==p+2 && /^HostName/{
$0="HostName 1.1.1.1"; p=0} 1' ~/.ssh/config > $$.tmp && mv $$.tmp ~/.ssh/config
Taken from this SO question.
Now I would like to replace the following:
dev-api2 with $shn
1.1.1.1 with $privateip
But now when I run the awk command it does nothing.
These are the values of the variables:
shn=dev-api2
privateip=3.3.3.3
Upvotes: 0
Views: 29
Reputation: 74705
You will need to use a dynamic regex instead of a literal one:
awk -v shn="$shn" -v privateip="$privateip" '
$0 ~ shn "\\.company-private" { p = NR }
p && NR == p + 2 && /^HostName/ { $0 = "HostName " privateip; p = 0 } 1'
Instead of /regex/
, use $0 ~ "string"
. The string will be evaluated and transformed into a regex, so you need to double-escape \\.
Upvotes: 2
Reputation: 4650
Use \":
awk "/dev-api2\.company-private/{p=NR} p && NR==p+2 && /^HostName/{
$0=\"HostName 1.1.1.1\"; p=0} 1" ~/.ssh/config > $$.tmp && mv $$.tmp
~/.ssh/config
Upvotes: -1