仅当行不存在时才将其追加到文件中

编程入门 行业动态 更新时间:2024-10-11 21:31:41
本文介绍了仅当行不存在时才将其追加到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要在配置文件的末尾添加以下行:

I need to add the following line to the end of a config file:

include "/configs/projectname.conf"

到名为lighttpd.conf

我正在考虑使用sed来做到这一点,但我不知道该怎么做.

I am looking into using sed to do this, but I can't work out how.

我如何只在行不存在的情况下插入它?

How would I only insert it if the line doesn't already exist?

推荐答案

只需保持简单:)

grep + echo 就足够了:

grep -qxF 'include "/configs/projectname.conf"' foo.bar || echo 'include "/configs/projectname.conf"' >> foo.bar

  • -q安静
  • -x匹配整行
  • -F模式是纯字符串
  • linux.die/man/1/grep
    • -q be quiet
    • -x match the whole line
    • -F pattern is a plain string
    • linux.die/man/1/grep
    • 包含@cerin和@ thijs-wouters建议.

      incorporated @cerin and @thijs-wouters suggestions.

更多推荐

仅当行不存在时才将其追加到文件中

本文发布于:2023-07-21 03:36:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1173699.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:将其   不存在   时才   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!