Sed 如何从路由字符串中找到模式,并在bashshell中编辑下一行?
上面是目标文件Sed 如何从路由字符串中找到模式,并在bashshell中编辑下一行?,sed,Sed,上面是目标文件test.txt。我需要找到patternbaseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/,将下一行enabled=0替换为enabled=1 我尝试了sed'@baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/@!BNcenabled=1'test.txt但失败 注意:不能使用其他delimeter,如@
test.txt
。我需要找到patternbaseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/
,将下一行enabled=0替换为enabled=1
我尝试了sed'@baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/@!BNcenabled=1'test.txt
但失败
注意:不能使用其他delimeter,如@
而不是/
,因为
这不是替换命令
提前谢谢 这可能会解决它找到“baseurl=”然后抓住下一行并用“enabled=1”替换“enable=0”:
cat test.txt
baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/
enabled=0
请随意将初始正则表达式更改为您提到的行。我只是想展示一下一般的解决方案
我希望这有帮助 如果您对
awk
没问题,请尝试以下内容
sed '/baseurl=/ {N;s/enabled=0/enabled=1/;}' test.txt
如果要将输出保存到输入文件本身,请在上述代码中附加>临时文件和&mv临时文件输入文件
awk '
/baseurl=http:\/\/repo\.mysql\.com\/yum\/mysql-5\.5-community\/el\/7\/\$basearch\//{
print
flag=1
next
}
flag && /enabled/{
print "enabled=1"
flag=""
next
}
1
' Input_file
这个答案来自RavinderSingh13和WiktorStribiżew,我只是来解释一下。出于某种原因,我只能使用斜杠作为分隔符,如果在匹配(/
)命令中使用斜杠以外的分隔符,请在分隔符之前使用反斜杠。(s
substitution命令不同;它可以使用任何标点符号作为分隔符,而不使用任何反斜杠。)@WiktorStribiżew,原因是什么?@kittygirl看到tripleee的注释,它不是替换命令。
sed -E '/baseurl=http:\/\/repo\.mysql\.com\/yum\/mysql-5\.5-community\/el\/7\/\$basearch\//!b;n;cenabled=1' test.txt > temp_file && mv temp_file test.txt