【问题标题】:Cutting the top two lines after every six lines from a file containing 180 lines从包含 180 行的文件中,每六行剪切前两行
【发布时间】:2013-09-25 14:29:21
【问题描述】:

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

【问题讨论】:

    标签: unix sed cut


    【解决方案1】:

    您可以使用行号除法的模数/ 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
    

    【讨论】:

    • 非常感谢这个解决方案,我可以对每组剩余的 4 行进行一些修改吗?我如何才能避免前两行受到影响?
    • @awrit 你是什么意思?所以输出将是7, 8...?
    • 哦,我有一组 180 个命令来配置 180 个链接。现在,我的实际要求是对第一个和第二个命令进行更改,在 6 个命令后递归地重复相同的命令。所以我想知道我该怎么做这个操作。
    • 是的,你可以。 awk 'NR%6==1 || NR%6==2 {do some} !(NR%6==1 || NR%6==2) {do some other}'
    • Exatly Jotne,甚至awk 'NR%6==1 || NR%6==2 {do some; next} {do some other}'
    【解决方案2】:

    您已经使用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 行进行一些修改吗?我如何才能避免前两行受到影响?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    相关资源
    最近更新 更多