简体   繁体   English

Shell脚本在文件中查找和替换

[英]shell script find and replace in file

I have a file postmaster.log, in which I need to find pattern and change its value Pattern I need to find is 我有一个文件postmaster.log,我需要在其中找到模式并更改其值。我需要找到的模式是

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尝试将MaxValue = 3更改为MaxValue = 0

 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. 但这仅在MaxValue = 3表示任何其他值时才起作用,它将不起作用。

use a regexp to catch MaxValue= followed by any number... 使用正则表达式来捕获MaxValue=后跟任意数字...

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

should work. 应该管用。

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 . 它将找到以MaxValue=开头的所有行,并用MaxValue=0替换每行。

您也可以限制sed的行数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM