例如,如果我有此文件:

The green horse is blue localhost
my pants is dirty localhost
your eyes is so beautiful localhost
The best area is localhost

我想在每次“本地主机”模式搜索后在此文件中添加文本(192.168.25.50)。
  • 我不想创建新行
  • 我不想在每行结尾处的上插入,但仅在模式
  • 之后插入
  • 我不想使用awk,我需要知道sed命令是否可以执行此操作

  • 实际上,我的结果必须如下所示:
    The green horse is blue localhost 192.168.25.50
    my pants is dirty localhost 192.168.25.50
    your eyes is so beautiful localhost 192.168.25.50
    The best area is localhost 192.168.25.50
    

    存在很多sed问题,但都存在特定的上下文,这对我的简单基础知识无济于事。我已经可以在相同文件中的模式之后插入文本,但是永远不能在同一行中插入文本。

    您能解释一下如何用sed命令在与sed命令本身使用的模式相同的行中插入吗?

    最佳答案

    使用GNU sed:

    sed 's/\blocalhost\b/& 192.168.25.50/' file
    


    如果要“就地”编辑文件,请使用sed的选项-i

    关于sed - 在模式搜索的同一行上插入sed文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35252285/

    10-12 17:43
    查看更多