bash 如何删除文本文件中的每 X 行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9894986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I delete every Xth line in a text file?
提问by Ingo
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
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?
例如,如何轻松删除文件中的每两行或 10 行中的每 9 行?例如,是否可以使用 bash 脚本?
Background: the file is very large but I need much less data to plot. Note that I am using Ubuntu/Linux.
背景:文件非常大,但我需要的数据要少得多。请注意,我使用的是 Ubuntu/Linux。
回答by jordanm
This is easy to accomplish with awk.
这很容易用 awk 完成。
Remove every other line:
删除每隔一行:
awk 'NR % 2 == 0' file > newfile
Remove every 10th line:
每 10 行删除一次:
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.
awk 中的 NR 变量是行号。awk 中 { } 之外的任何内容都是有条件的,默认操作是打印。
回答by sorpigal
How about perl?
perl呢?
perl -n -e '$.%10==0&&print' # print every 10th line
回答by sorpigal
You could possibly do it with sed, e.g.
你可以用sed来做,例如
sed -n -e 'p;N;d;' file # print every other line, starting with line 1
If you have GNU sed it's pretty easy
如果你有 GNU sed,那就很容易了
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 2
回答by potong
This might work for you (GNU sed):
这可能对你有用(GNU sed):
seq 10 | sed '0~2d' # delete every 2nd line
1
3
5
7
9
seq 100 | sed '0~10!d' # delete 9 out of 10 lines
10
20
30
40
50
60
70
80
90
100
回答by Mat
Try something like:
尝试类似:
awk 'NR%3==0{print awk 'NR%10<9{print nawk -f awkfile.awk [filename]
awkfile.awk contents
BEGIN {
if (!lines) lines="3 4 7 8"
n=split(lines, lA, FS)
for(i=1;i<=n;i++)
linesA[lA[i]]
}
!(FNR in linesA)
}' file
}' file
This will print one line in three. Or:
这将打印一行三。或者:
:%!awk NR\%2 or :%!awk NR\%2
will print 9 lines out of ten.
将打印 10 行中的 9 行。
回答by broguyman
You can use a awk and a shell script. Awk can be difficult but...
您可以使用 awk 和 shell 脚本。awk 可能很困难,但是...
This will delete specific lines you tell it to:
这将删除您告诉它的特定行:
##代码##Also I can't remember if VIM comes with the standard Ubuntu or not. If not get it.
另外我不记得 VIM 是否带有标准的 Ubuntu。如果没有得到。
Then open the file with vim vim [filename]
然后用 vim vim [filename] 打开文件
Then type
然后输入
##代码##This will delete every other line. Just change the 2 to another integer for a different frequency.
这将删除每隔一行。只需将 2 更改为不同频率的另一个整数。