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

浪尽此生 提交于 2019-12-24 16:57:46

问题


How can I replace the final character of a file with a ]?

For example, if the document content looked like this:

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.

I would want it to look like this afterwards:

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:


You may specify line numbers, ranges or even the last line where you want to perform search and replace operations with sed:

This will replace the final , char with ] only on the last line:

sed '$ s/,$/]/'

Here, the $ char tells sed to only replace on the last line.

The sed '1 s/,$/]/' command will do that only on Line 1, and sed '1,4 s/,$/]/' will do that on lines 1 through 4.



来源:https://stackoverflow.com/questions/52779284/how-can-i-replace-the-final-character-in-a-document-with-a

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!