0

我想从一个文件(一组 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 个命令重复一次。

4

2 回答 2

3

你已经得到了来自@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
56
于 2013-09-20T09:13:00.567 回答
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
于 2013-09-20T09:03:14.973 回答