user189942
user189942

Reputation: 361

Replace a variable with text (sed)

I have to find a specific text in the file and then replace that text with some new text.

The contents of the file are:

host=100
servers=4
clients=70

I have tried this:

var=$(grep "servers=" /path/to/file)
sed -i "s/${var}/servers=5/g" /path/to/file

But it gives me the error:

sed: -e expression #1, char 2: unterminated `s' command

Note: All I want is to update the value of each of the variable i.e. servers=4 should be replaced by servers=5.

Please help me figure out the solution.

Thanks.

Upvotes: 0

Views: 71

Answers (2)

Melebius
Melebius

Reputation: 6695

The output of grep ends with a newline character. sed expects the whole command on one line or escaping line breaks.

However, you can easily achieve the complete task with sed only:

sed -i 's/^servers=[0-9]*$/servers=5/' /path/to/file

Upvotes: 3

chw21
chw21

Reputation: 8140

sed -i.bak "s/servers=[0-9]*/servers=5/" /path/to/file

Upvotes: 3

Related Questions