Ravikanth
Ravikanth

Reputation: 383

Replace a string using sed

I want to replace a string ip_ttl="1" with ip_ttl="2" using sed.

I've tried sed -i "s/ip_ttl="1"/ip_ttl="2"/g" - but its not working.

Please help!

Upvotes: 0

Views: 90

Answers (3)

Avinash Raj
Avinash Raj

Reputation: 174696

Put your sed code inside single quotes, because your code already contains double quotes.

sed -i 's/ip_ttl="1"/ip_ttl="2"/g' file

If you put your code within two double quotes, sed would terminate the program once another double quote was reached. So " before 1 was reached, it would consider as the end and terminates the program.

Update:

If the number always changes then it's better to define the pattern which matches any number.

sed -i 's/ip_ttl="[0-9]\+"/ip_ttl="2"/g' file

Upvotes: 2

baf
baf

Reputation: 4661

If you are using quotation marks in your pattern either escape double quotes in pattern:

sed -i "s/ip_ttl=\"1\"/ip_ttl=\"2\"/g"

or enclose whole pattern in single quotes:

sed -i 's/ip_ttl="1"/ip_ttl="2"/g'

Upvotes: 1

jcuenod
jcuenod

Reputation: 58375

Alternatively you can escape the quotation marks

sed -i "s/ip_ttl=\"1\"/ip_ttl=\"2\"/g" file

Sometimes that's useful because you have both single and double quotes in a string you're selecting for.

Upvotes: 0

Related Questions