如何如果没有找到匹配的SED中添加一行 [英] How to add a line in sed if not match is found

查看:2797
本文介绍了如何如果没有找到匹配的SED中添加一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的sed命令在配置文件来替换一些参数:

i am using the following sed command to replace some parameters in a config file:

sed -i 's/^option.*/option=value/g' /etc/fdm_monitor.conf

现在我有一个问题。如果该行不存在,我想将它添加到文件底部。

Now i have one problem. If the line does not exist i want to add it to the bottom of the file.

我用的popen了一个C程序调用此。
我试着用awk和谷歌没有给我一个正确的结果。

I am calling this with a popen out of a c program. I tried using awk and google did not give me a proper result.

先谢谢了。

问候
萨沙

推荐答案

试试这个:

grep -q '^option' file && sed -i 's/^option.*/option=value/' file || echo 'option=value' >> file

这篇关于如何如果没有找到匹配的SED中添加一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆