Unix 从包含180行的文件中,每六行剪切一行后的前两行

Unix 从包含180行的文件中,每六行剪切一行后的前两行,unix,sed,cut,Unix,Sed,Cut,我想从一个文件中提取最上面的两行(一组180行),这样,如果我将文件分组到6-6行的集合中,就会得到前两行作为输出。所以我应该可以得到第一,第二,第七,第八等等。我尝试使用sed来实现这一点,但没有得到所需的输出 有人能提出在这里要实现的逻辑吗 我的要求是对前两行进行一些修改(比如删除某些字符),每6行一组 例如: This is line command 1 for my configuration This is line command 2 for my configuration Thi

我想从一个文件中提取最上面的两行(一组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个命令必须重复一次。

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

awk 'NR%6==1 || NR%6==2' file
NR
表示记录数,在本例中为“行数”,因为默认记录为行
|
代表“或”。最后,不需要编写任何
print
,因为这是
awk
的默认行为

例子:
根据您的更新,这可以:

$ 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

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

awk 'NR%6==1 || NR%6==2' file
NR
表示记录数,在本例中为“行数”,因为默认记录为行
|
代表“或”。最后,不需要编写任何
print
,因为这是
awk
的默认行为

例子:
根据您的更新,这可以:

$ 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

您已经使用
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

您已经使用
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

非常感谢这个解决方案,我可以对每组剩下的4行进行一些修改吗?如何防止前两行受到影响?非常感谢此解决方案,我是否可以对每组的其余4行进行一些修改?如何防止前两行受到影响?非常感谢此解决方案,我是否可以对每组的其余4行进行一些修改?我怎样才能避免前两行受到影响?@awrit你是什么意思?所以输出将是
7,8…
?哦,我有一组180个命令来配置180个链接。现在,我的实际需求是对第一个和第二个命令进行更改,这会在6个命令之后递归重复相同的操作。所以我想知道我怎么做这个手术。是的,你可以<代码>awk'NR%6==1 | | NR%6==2{做点什么}!(NR%6==1 | | NR%6==2){do some other}'examply Jotne,甚至
awk'NR%6==1 | | NR%6==2{do some;next}{do other}'
非常感谢这个解决方案,我还可以对每组剩下的4行进行一些修改吗?我怎样才能避免前两行受到影响?@awrit你是什么意思?所以输出将是
7,8…
?哦,我有一组180个命令来配置180个链接。现在,我的实际需求是对第一个和第二个命令进行更改,这会在6个命令之后递归重复相同的操作。所以我想知道我怎么做这个手术。是的,你可以<代码>awk'NR%6==1 | | NR%6==2{做点什么}!(NR%6==1 | | NR%6==2){做一些其他的}example Jotne,甚至
awk'NR%6==1 | | NR%6==2{做一些;下一步}{做一些其他的}