如何删除文本文件中的每 X 行? [英] How can I delete every Xth line in a text file?

查看:15
本文介绍了如何删除文本文件中的每 X 行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个包含科学数据的文本文件,例如:

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

5.787037037037037063e-02 2.048402977658663748e-01
1.157407407407407413e-01 4.021264347118673754e-01
1.736111111111111049e-01 5.782032163406526371e-01

例如,我如何轻松删除文件中的每两行或每 10 行中的 9 行?例如,是否可以使用 bash 脚本?

How can I easily delete, for instance, every second line, or every 9 out of 10 lines in the file? Is it for example possible with a bash script?

背景:文件非常大,但我需要的数据要少得多.请注意,我使用的是 Ubuntu/Linux.

Background: the file is very large but I need much less data to plot. Note that I am using Ubuntu/Linux.

推荐答案

这用 awk 很容易实现.

This is easy to accomplish with awk.

每隔一行删除:

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

每 10 行删除一次:

Remove every 10th line:

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

awk 中的 NR 变量是行号.awk 中 { } 之外的任何内容都是条件,默认操作是打印.

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

这篇关于如何删除文本文件中的每 X 行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆