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

How can I replace the final character in a document with a ]?

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

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

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]

您可以使用 sed:

指定要执行搜索和替换操作的行号、范围甚至最后一行

这将在最后一行将最后的 , 字符替换为 ]

sed '$ s/,$/]/'

这里,$ 字符告诉 sed 只替换最后一行。

sed '1 s/,$/]/' 命令只会在第 1 行执行此操作,sed '1,4 s/,$/]/' 会在第 1 行到第 4 行执行此操作。