0

I have a file postmaster.log, in which I need to find pattern and change its value Pattern I need to find is

MaxValue=3 #this could be any value not just 3

I need to change its value to

MaxValue=0

Issue is there are also patterns like

"MaxValueSet=3" and "MaxValue is currently low" 

Which are also getting replaced.I only has to change MaxValue=3 to MaxValue=0 I tried using sed

 sed -i 's/MaxValue=3/MaxValue=0/g' /home/postmaster.log

But this only works if MaxValue=3 for anyother value it won't work.

3 Answers 3

4

use a regexp to catch MaxValue= followed by any number...

s/MaxValue=[0-9]+/MaxValue=0/g

should work.

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

5 Comments

I tried and it replaces whole line, I just want to replace MaxValue=3 with MaxValue=0, not the whole line. This works for whole line replacement though
@JumpOffBox: Are you sure about that? This should only replace MaxValue= plus one or more digits. Can you give an example where it seems to replace more than that?
I am sure. I tried and its not working. Example of line from file VerifyValue=0 ; VerifyValue should be set to 0 MaxValue=5 ; Maximum number of Value for kernel jobs
if I use sed -i "s/MaxValue=.*/MaxValue=0/g", it works but it will replace the whole line not just MaxValue. Are there any other way aprt from sed
sed -i -r 's/MaxValue=[0-9]+/MaxValue=0/g' /home/postmaster.log
2

It sounds like you want

sed -i 's/^MaxValue=.*/MaxValue=0/' /home/postmaster.log

which will find all lines that begin with MaxValue=, and replace each of those lines with MaxValue=0.

4 Comments

As long as the OP wants to replace the entire line.
@jahroy: Yes, absolutely. That's why I always make a point of explaining what the code does, so the OP can judge whether that's actually what (s)he wants.
I tried and it replaces whole line, I just want to replace MaxValue=3 with MaxValue=0, not the whole line. This works for whole line replacement though
@JumpOffBox: Then Jean-Baptiste Yunès' answer is what you want.
1

You can restrict the lines sed works on as well:

sed -i '/^MaxValue=/s/=[[:digit:]][[:digit:]]*/=0/' /home/postmaster.log

2 Comments

I tried and it replaces whole line, I just want to replace MaxValue=3 with MaxValue=0, not the whole line. This works for whole line replacement though
I've updated to only replace a positive integer value on the RHS. Other regular expressions can be given to more closely match your usage if necessary.

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.