linux - 在 Bash 中通过正则表达式从字符串中删除后缀?

标签 linux bash shell

<分区>

我有一个以偶数个 0 结尾的字符串。我想通过正则表达式 (00)* 删除这些 0:

ababababab000000 => ababababab
abababab00       => abababab
abab000000000    => abab0
aba00000         => aba0

这在 Bash 中如何完成?通常,我将如何使用正则表达式删除任何后缀?

最佳答案

使用 sed -r 搜索并用 s/regex/replacement/ 替换任意正则表达式。 $ 将正则表达式锚定到每行的末尾。

$ sed -r 's/(00)*$//' <<< 'ababababab000000'
ababababab
$ sed -r 's/(00)*$//' <<< 'abababab00'
abababab
$ sed -r 's/(00)*$//' <<< 'abab000000000'
abab0
$ sed -r 's/(00)*$//' <<< 'aba00000'
aba0

关于linux - 在 Bash 中通过正则表达式从字符串中删除后缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50620708/

相关文章:

bash - 如何在单个循环的每次迭代中从多个文件中的每一个读取一行?

linux - 删除按日期滚动的旧文件,使 Cronjob 中的某些文件无法正常工作

linux - Bash(或其他 shell ): wrap all commands with function/script

linux - "struct file_operations"参数是什么?

bash - 在 shell (bash) 中如何在管道中执行多个命令?

linux - linux shell 编程中的日历问题

windows - 在 Windows 上运行 bash 脚本时,Git Bash 与 Slash '/' 混淆

bash - 从 bash 执行 ksh 脚本

asp.net - 是否可以在 Linux 上托管 ASPX 网站?

c++ - 如何使用 _GLIBCXX_DEBUG 构建 Boost 版本?