How can I delete every Xth line in a text file?

后端 未结 6 1511
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 05:07

Consider a text file with scientific data, e.g.:

5.787037037037037063e-02 2.048402977658663748e-01
1.157407407407407413e-01 4.021264347118673754e-01
1.736111         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 05:29

    This is easy to accomplish with awk.

    Remove every other line:

    awk 'NR % 2 == 0' file > newfile
    

    Remove every 10th line:

    awk 'NR % 10 != 0' file > newfile
    

    The NR variable in awk is the line number. Anything outside of { } in awk is a conditional, and the default action is to print.

提交回复
热议问题