bash - 删除行的第一部分

标签 bash sed

给定以下文件

$ cat a.txt
The quick brown fox jumps over the lazy dog

我可以使用 sed 轻松删除第一部分

$ sed 's/.*fox//' a.txt
 jumps over the lazy dog

我试图巧妙地使用 D 选项

D      Delete  up  to  the first embedded newline in the pattern space.

例子

$ sed 's/fox/\n/; D' a.txt

但是这没有输出。是否可以使用 D 选项通过 sed 删除一行的第一部分,这与我尝试过的类似?

如果 fox 之前的部分比之后的部分短,这会起作用。

$ sed 's/fox/\r/' a.txt
 jumps over the lazy dog

最佳答案

一种方法:

sed '/fox/{s/fox/\n/;D}' input 

关于bash - 删除行的第一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15062361/

相关文章:

linux - 在 shell 脚本中终止程序 (CTRL+C)

bash - 重用或重复管道输入作为命令中的参数(给出的示例)

bash - 在 sed 追加中插入前导回车符

shell - POSIX:abcdef 到 ab bc cd de ef

bash - 如何修复 Endeca 中的 "Failed to obtain lock"错误?

linux - 通过配对 linux 中的值将 2 个文件的内容复制到新文件中

bash - 如何从 bash 脚本启动多个 screen session

linux - 使用 sed 匹配模式时如何打印最后几行?

bash - 用于从两个日期范围之间的文件中提取数据的 Shell 脚本

sed - 使用sed在一行中重复字符串