bash:从文本文件中过滤掉连续行
我想从许多文件中删除段落的每个实例。我将段落称为行序列。
例如:
my first line my second line my third line the fourth 5th and last
问题是我只想在它们作为一组出现时删除它们。例如,如果
my first lineappears alone I don't want to delete it.
I want to delete from many files each instance of a paragraph. I call paragraph a sequence of lines.
For example:
my first line my second line my third line the fourth 5th and last
the problem is that I only want to delete them when they appear as a group. For example, if
my first line
appears alone I don't want to delete it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@OP,我看到你接受了你的段落句子是“硬编码”的答案,所以我认为这些段落总是相同的?确实如此,您可以使用
grep
。将要删除的段落存储在文件中,例如“filter”,然后使用 grep 的-f
和-v
选项来完成这项工作,@OP, i see you accepted the answer whereby your paragraph sentences are "hardcorded", so i assume those paragraphs are always the same? its that's true, you can use
grep
. Store the paragraph you want to get rid of in a file eg "filter", then use-f
and-v
option of grep to do the job,如果您能够使用 Perl,您可以像这样一行完成:
解释位于 perlrun< /a>:
示例输入:
输出:
If you are able to use Perl, you can do it in one line like this:
the explanation is in perlrun:
Sample input:
Output:
你可以用 sed 来做到这一点:
You can do it with sed: