从文本文件中删除最后 2 个字符:shell 脚本 [英] delete the last 2 characters from a text file: shell script
本文介绍了从文本文件中删除最后 2 个字符:shell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从 shell 脚本中的文本文件中删除最后 2 个字符.知道我该怎么做吗?
I need to delete the last 2 characters from a text file in shell script. Any idea about how I can do it?
推荐答案
使用 sed
删除最后一行 only 的最后两个字符:
Delete the last two characters on the last line only with sed
:
$ sed '$s/..$//' file
如果您对更改感到满意,请使用 -i
将它们存储回文件:
If you are happy with changes then use -i
to store them back to the file:
$ sed -i '$s/..$//' file
如果你想删除每一行的最后两个字符,它会是:
If you wanted to delete the last two characters on every line it would be:
$ sed 's/..$//' file
再次使用 -i
将更改存储回文件:
Again use -i
to store the changes back to the file:
$ sed -i 's/..$//' file
这篇关于从文本文件中删除最后 2 个字符:shell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文