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

时间:2013-09-20 09:01:45

标签: unix sed cut

我想从文件中提取前两行(一组180行),这样,如果我将文件分组为6-6行,我将前两行作为输出。所以我应该能够获得第1名,第2名,然后是第7名,第8名,依此类推。我尝试使用sed但没有获得所需的输出。

有人可以建议在这里实施的逻辑

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

示例:

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

我想要的输出是:

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个命令。

2 个答案:

答案 0 :(得分:3)

你已经使用awk得到了@fedorqui的答案。这是一种使用sed的方法。

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

答案 1 :(得分:2)

您可以使用行号/ 6的除法模数来进行。如果是1或2,则打印该行。否则,不要。

awk 'NR%6==1 || NR%6==2' file

NR代表记录数,在这种情况下是“行数”,因为默认记录是一行。 ||代表“或”。最后,不需要编写任何print,因为它是awk的默认行为。

实施例

$ 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

根据您的更新,可以实现:

$ 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