如何用sed删除每隔一行? [英] How to remove every other line with sed?
本文介绍了如何用sed删除每隔一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 sed 删除所有奇数行?
How can I remove every odd line, using sed?
remove
keep
remove
keep
remove
...
推荐答案
GNU sed 有一个合适的寻址模式:
GNU sed has a suitable addressing mode:
sed -n '1~2!p' file
这意味着,从第 1 行开始,到第 2 步,打印所有其他行.
which means, starting from line 1, and with step 2, print all other lines.
等效地,您可以删除 -n
,并删除匹配的行:
Equivalently, you can drop the -n
, and delete matching lines:
sed '1~2d'
也可以使用awk来完成:
It can also be done using awk:
awk 'NR%2==0' file
(当行号是 2 的倍数时,打印该行)
(Whenever line number is a multiple of 2, print the line)
这篇关于如何用sed删除每隔一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文