3

I have to replace a specific line of a file with some other value. for eg: I have a file with content as below:

request.timing=0/10 * * * * * ?

Now, I want to replace 10 (or to be specific, any value in that place) with a dynamic value. Eg, if I want to replace that with 20, the file should be updated as:

request.timing=0/20 * * * * * ?

Can someone help me? I am using sed as below:

sed -i "s/request.timing=0\/??/request.timing=0\/$poller"

where poller is dynamic value we pass.

0

3 Answers 3

1

First of all, you must check the characters the variable $poller has, because sed can interpret any character in there as a special one. Once you have checked that, use a sed separator which is not inside $poller. Let's suppose we can use /:

sed -r 's/(request.timing=0\/)[0-9]{2}/\1'"$poller"'/g'

How does it works

-r option activates extended regular expressions so you can use things like [0-9]{2}, and with ( ... ) you're capturing the string to use in the replacement site.

Sign up to request clarification or add additional context in comments.

Comments

0
$ echo 'request.timing=0/10 * * * * * ?' |
sed -r 's:(request.timing=0/)[0-9]+:\1135:'
request.timing=0/135 * * * * * ?

$ poller="135"
$ echo 'request.timing=0/10 * * * * * ?' |
sed -r 's:(request.timing=0/)[0-9]+:\1'"$poller"':'
request.timing=0/135 * * * * * ?

Comments

0

The reason that you were having troubles is that ? is not valid in the regular expression. Use . instead. ? is used to say the previous character may be present 0 or 1 times.

sed -i "s/request.timing=0\/../request.timing=0\/$poller"

(That's not to say the other answers aren't correct!)

1 Comment

Hi.. I tried both answers @miken32 and Ed Morton ..both worked.. Thanks for the swift response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.