代码之家  ›  专栏  ›  技术社区  ›  Mus mzuba

如何用“]”替换文档中的最后一个字符?

  •  1
  • Mus mzuba  · 技术社区  · 6 年前

    如何将文件的最后一个字符替换为 ] ?

    例如,如果文档内容如下所示:

    This is the first line with a full stop.
    This is the second line with a full stop.
    This is the third line with square bracket.
    

    This is the first line with a full stop.
    This is the second line with a full stop.
    This is the third line with square bracket]
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   Wiktor Stribiżew    6 年前

    您可以指定行号、范围,甚至是要执行搜索和替换操作的最后一行 sed

    , ] 只有 最后一行:

    sed '$ s/,$/]/'
    

    这里,那个 $ char告诉我们 只替换最后一行。

    这个 sed '1 s/,$/]/' 命令只能在第1行执行,并且 sed '1,4 s/,$/]/'