我想从一个文件中提取前两行(一组180行),这样,如果我将文件分组为6-6行,我将得到前两行作为输出。所以我应该可以得到第一,第二,然后第七,第八,等等。为此,我尝试使用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条重复一次。
发布于 2013-09-20 09:03:14
你可以用线数/ 6除法的模数来做。如果是1或2,那就打印这条线。否则,不要。
awk 'NR%6==1 || NR%6==2' fileNR代表记录的数量,在本例中是“行数”,因为默认记录是一行。||代表“或”。最后,不需要编写任何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发布于 2013-09-20 09:13:00
您已经收到了@fedorqui使用awk的答复。下面是一种使用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
56https://stackoverflow.com/questions/18912736
复制相似问题