How to remove every other line with sed?

前端 未结 5 487
春和景丽
春和景丽 2020-12-04 14:16

How can I remove every odd line, using sed?

remove
keep
remove
keep
remove
...
5条回答
  •  醉话见心
    2020-12-04 14:52

    GNU sed has a suitable addressing mode:

    sed -n '1~2!p' file
    

    which means, starting from line 1, and with step 2, print all other lines.

    Equivalently, you can drop the -n, and delete matching lines:

    sed '1~2d'
    

    It can also be done using awk:

    awk 'NR%2==0' file
    

    (Whenever line number is a multiple of 2, print the line)

提交回复
热议问题