考虑具有科学数据的文本文件,例如:
5.787037037037037063e-02 2.048402977658663748e-01
1.157407407407407413e-01 4.021264347118673754e-01
1.736111111111111049e-01 5.782032163406526371e-01例如,我如何轻松地删除文件中的每一行或每10行中的9行?例如,使用bash脚本可以吗?
背景:文件很大,但我需要的数据要少得多。请注意,我使用的是Ubuntu/Linux。
发布于 2012-03-27 18:07:37
这很容易用awk实现。
移除每一行:
awk 'NR % 2 == 0' file > newfile每隔10行删除一次:
awk 'NR % 10 != 0' file > newfileawk中的NR变量是行号。在awk中,{}之外的任何内容都是有条件的,默认操作是打印。
发布于 2012-03-27 18:26:29
perl怎么样?
perl -n -e '$.%10==0&&print' # print every 10th line发布于 2012-03-27 18:04:49
你可以用sed做这件事。
sed -n -e 'p;N;d;' file # print every other line, starting with line 1如果你有GNU,那就很容易了
sed -n -e '0~10p' file # print every 10th line
sed -n -e '1~2p' file # print every other line starting with line 1
sed -n -e '0~2p' file # print every other line starting with line 2https://stackoverflow.com/questions/9894986
复制相似问题