用 sed 删除大块的行 [英] Removing chunks of lines with sed

查看:50
本文介绍了用 sed 删除大块的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图浏览一个文件,并在每组 40 行中保留一组连续的 4 行.

I am trying to go through a file and keep a consecutive group of 4 rows out of each consecutive group of 40 rows.

所以在整个文件中,我会保留行 1-441-4481-84
我尝试使用 sed,但我真的只能删除特定的行,而不能执行这样的模式.

So in the whole file, I would keep rows 1-4, 41-44, 81-84, etc.
I tried using sed, but I am really only able to remove specific rows, not do a pattern like this.

非常感谢...

推荐答案

这个awk应该做的:

awk 'NR%40==1 || NR%40==2 || NR%40==3 || NR%40==4' file

循环版本:

awk '{for (i=1;i<5;i++) if (NR%40==i) print $0}' file

在我测试了各种解决方案后发现这应该有效:

Found this should work after I tested various solution:

awk 'NR%40~/^[1-4]$/' file

<小时>

测试

seq 1 100 > file

awk 'NR%40~/^[1-4]$/' file
1
2
3
4
41
42
43
44
81
82
83
84

这篇关于用 sed 删除大块的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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