如何删除文件最后一行的最后一个字符? [英] How can I remove the last character of the last line of a file?

查看:488
本文介绍了如何删除文件最后一行的最后一个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件中有以下几行:

I have this set of lines in a file:

{info},
{info},
{info},
{info},

并且我想要这样的文件,但不带最后的,":

and I want the file like this without the last ",":

{info},
{info},
{info},
{info}

我该如何使用bash进行操作?有什么主意吗?

How can I do it in bash? Any idea?

推荐答案

您可以使用sed:

sed '$ s/.$//' your_file

  • 第一个$是告诉sed仅匹配最后一行
  • s用于替代",请注意最后两个/ s
  • 之间的空字符串
  • .$是与文件中的最后一个字符匹配的正则表达式
    • First $ is to tell sed to match only last line
    • s is for "substitute", note the empty string between the two last /s
    • .$ is a regex that matches the last character in the file
    • 请注意,您可以使用所需的任何分隔符代替/,例如,可以重写表达式:

      Note that you can use whatever separator you want instead of /, for example you can rewrite the expression:

      sed '$ s-.$--' your_file
      

      这篇关于如何删除文件最后一行的最后一个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆