bash - 删除字符串最后一个大小写之前的所有行

标签 bash sed

我将如何删除最后一次出现的字符串之前的所有行。就像我有一个看起来像的文件

Icecream is good
And
Chocolate is good
And
They have lots of sugar

If I want all lines after and including the last occurrence of "And" what's the cleanest way to do this? Specifically, I want

And
They have lots of sugar

我正在做 sed -n -E -e '/And/,$p' file 但我看到这给了我第一次出现。

最佳答案

这可能对你有用(GNU sed):

sed -n '/And/h;//!H;$!d;x;//p' file

用包含 And 的行替换保留空间中的任何内容。将所有其他行附加到保留空间。在文件末尾,将模式空间交换为保持空间,并打印出与所需字符串 And 匹配的结果。

关于bash - 删除字符串最后一个大小写之前的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30958584/

相关文章:

linux - grep -v 除了模式

linux - 有没有办法用 sed 将所有行附加到模式空间?

shell - 全局替换 shell 脚本不起作用

csv - 如何使用 awk 将列插入到 CSV 文件中

linux - 递归复制和重命名

linux - 如何在 curl 请求的数据中发布 URL

linux - bash 脚本中的 source/etc/profile

shell - 减去相应的行

bash - 如何删除包含特定字符串的所有行,但仅当后面的字符是 CJK 字符时?

Java 错误 : Could not find the main class