首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每隔6行从包含180行的文件中切割前两行

每隔6行从包含180行的文件中切割前两行
EN

Stack Overflow用户
提问于 2013-09-20 09:01:45
回答 2查看 646关注 0票数 0

我想从一个文件中提取前两行(一组180行),这样,如果我将文件分组为6-6行,我将得到前两行作为输出。所以我应该可以得到第一,第二,然后第七,第八,等等。为此,我尝试使用sed,但没有获得所需的输出。

请有人建议一下这里要实现的逻辑。

我的要求是对前两行进行一些修改(比如删除某些字符),每组6行。

示例:

代码语言:javascript
复制
This is line command 1 for my configuration
This is line command 2 for my configuration
This is line command 3 for my configuration
This is line command 4 for my configuration
This is line command 5 for my configuration
This is line command 6 for my configuration

我想要的输出是:

代码语言:javascript
复制
This is line command 1
This is line command 2 
This is line command 3 for my configuration
This is line command 4 for my configuration
This is line command 5 for my configuration
This is line command 6 for my configuration

这必须对180条命令中的每6条重复一次。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-20 09:03:14

你可以用线数/ 6除法的模数来做。如果是1或2,那就打印这条线。否则,不要。

代码语言:javascript
复制
awk 'NR%6==1 || NR%6==2' file

NR代表记录的数量,在本例中是“行数”,因为默认记录是一行。||代表“或”。最后,不需要编写任何print,因为它是awk的默认行为。

示例:

代码语言:javascript
复制
$ seq 60 | awk 'NR%6==1 || NR%6==2'
1
2
7
8
13
14
19
20
25
26
31
32
37
38
43
44
49
50
55
56

根据您的更新,这可以使它:

代码语言:javascript
复制
$ awk 'NR%6==1 || NR%6==2 {$6=$7=$8=$9} 1' file
This is line command 1   
This is line command 2   
This is line command 3 for my configuration
This is line command 4 for my configuration
This is line command 5 for my configuration
This is line command 6 for my configuration
This is line command 7   
This is line command 8   
This is line command 9 for my configuration
This is line command 10 for my configuration
This is line command 11 for my configuration
This is line command 12 for my configuration
This is line command 13   
This is line command 14   
This is line command 15 for my configuration
This is line command 16 for my configuration
This is line command 17 for my configuration
This is line command 18 for my configuration
This is line command 19   
This is line command 20   
This is line command 21 for my configuration
This is line command 22 for my configuration
This is line command 23 for my configuration
This is line command 24 for my configuration
票数 2
EN

Stack Overflow用户

发布于 2013-09-20 09:13:00

您已经收到了@fedorqui使用awk的答复。下面是一种使用sed的方法。

代码语言:javascript
复制
sed -n '1~6,2~6p' inputfile

# Example
$ seq 60 | sed -n '1~6,2~6p' 
1
2
7
8
13
14
19
20
25
26
31
32
37
38
43
44
49
50
55
56
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18912736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档