I have the following code:
function replaceappend() {
awk -v old="$2" -v new="$3" '
sub(old,new) { replaced=1 }
{ print }
END { if (!replaced) print new }
' "$1" > /tmp/tmp$$ &&
mv /tmp/tmp$$ "$1"
}
replaceappend "/etc/ssh/sshd_config" "Port" "Port 222"
It works perfectly but I am looking to modify it so that the awk command only finds a match if the line starts with that result. So if it's looking for the word "Port":
Port 123 # Test <- It would match this one
This is a Port <- It would not match this one
I've tried to look at other posts which ask about "Awk line starting with" such as this one, but I can't get my head around it: