如何使用sed剃掉最后一个字符? [英] How to shave off last character using sed?

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

问题描述

即从 ABCD -> ABC

推荐答案

你可以试试:

sed s'/.$//'

使用的正则表达式是 .$

The regex used is .$

  • . 是要匹配的正则表达式元字符任何东西(换行符除外)
  • $ 是行尾锚点.
  • . is a regex meta char to match anything (except newline)
  • $ is the end of line anchor.

通过使用 $ 我们强制 . 匹配最后一个字符

By using the $ we force the . to match the last char

这将删除最后一个字符,无论是什么:

This will remove the last char, be it anything:

$ echo ABCD | sed s'/.$//'
ABC
$ echo ABCD1 | sed s'/.$//'
ABCD

但是如果你想删除最后一个字符,只有当它是一个字母时,你可以这样做:

But if you want to remove the last char, only if its an alphabet, you can do:

$ echo ABCD | sed s'/[a-zA-Z]$//'
ABC
$ echo ABCD1 | sed s'/[a-zA-Z]$//'
ABCD1

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

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