shaolinLLama
shaolinLLama

Reputation: 55

command output > sed replace after a particular string

Hello I am trying to insert the output of command output > sed replace after a particular string in a file as part of user data on machine boot up

[centos@ip-192-168-2-22 scylla]$ sudo sed -i.bak 's/broadcast_rpc_address: : /broadcast_rpc_address:\/$hostname -i | awk '{print $2}'/' /etc/scylla/scylla.yaml

file is currently

broadcast_rpc_address:

replace with

broadcast_rpc_address: (the ip of the machine)

Upvotes: 0

Views: 116

Answers (1)

Nadav Har'El
Nadav Har'El

Reputation: 13771

Something like this should work:

sed -i.bak "s/broadcast_rpc_address:/broadcast_rpc_address: $(hostname -i)/" /etc/scylla/scylla.yaml

This will replace broadcast_rpc_address: by broadcast_rpc_address: $(hostname -i). Now, because this string is in double quotes - not single quotes - this tells the shell to interpret some magic sequences inside the string. In particular $(somecommand) means to run somecommand and insert its output into the string. Of course, change "hostname -i" in the command I gave above to anything else you want (it can even be an entire pipeline.

Your original attempt used something that started with $hostname. This syntax, $hostname, doesn't run the command hostname, but rather looks for a variable called hostname, which isn't what you wanted. You need the $(...) syntax instead. Your original attempt also had problems with nested quotes, which don't work.

Upvotes: 1

Related Questions