Reputation: 6159
I need to edit a files using shell script (sed), I can't seem to get it to work when I use "space" inside my string.
This is the bash script:
##Edit instancegroup files with relevant cluster
CLUSTER_NAME=clusters.dev10.k8s.local
oldcluster="kops.k8s.io/cluster:"
newcluster="kops.k8s.io/cluster: ${CLUSTER_NAME}"
sed -i 's@'${oldcluster}'@'${newcluster}'@g' myfile
This is myfile:
apiVersion: kops/v1alpha2
kind: InstanceGroup
metadata:
creationTimestamp: 2018-01-03T10:45:43Z
labels:
kops.k8s.io/cluster:
And this is the error I get:
administrator@JenkisSrv01:~/awscluster/instancegroup$ ./try.sh
sed: -e expression #1, char 43: unterminated `s' command
When I removing the space from line 4, it is working well:
newcluster="kops.k8s.io/cluster:${CLUSTER_NAME}"
Upvotes: 0
Views: 84
Reputation: 242343
Unquoted space separates parameters. Quote the variable:
sed -i 's@'"${oldcluster}"'@'"${newcluster}"'@g' myfile
or more readably
sed -i "s@$oldcluster@$newcluster@g" myfile
Note that .
has a special meaning in regexes, so kops.k8s.io/cluster:
matches e.g. kopsXk8sXio/cluster:
. You need to backslash the dots to force the literal meaning.
Upvotes: 3
Reputation: 19335
add double quotes around variables
sed -i 's@'"${oldcluster}"'@'"${newcluster}"'@g' myfile
Upvotes: 0