Linux 从文本文件中删除奇数或偶数行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21309020/
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
Remove odd or even lines from a text file
提问by SamuelNLP
I need to remove odd lines in a text file to make a down-sampling. I've found this command,
我需要删除文本文件中的奇数行以进行下采样。我找到了这个命令,
awk 'NR%2==0' file
but it only prints the odd lines in the terminal. How to really remove them?
但它只在终端中打印奇数行。如何真正去除它们?
I don't really care for even or odd, I want them removed from the file or printed in another file. This only prints them in the terminal.
我真的不关心偶数或奇数,我希望它们从文件中删除或打印在另一个文件中。这只会在终端中打印它们。
采纳答案by Thor
awk
awk
The %is a modulus operator and NRis the current line number, so NR%2==0is true only for even lines and will invoke the default rule for them ({ print $0 }). Thus to save only the even lines, redirect the output from awkto a new file:
这%是一个模数运算符并且NR是当前行号,因此NR%2==0仅对偶数行才为真,并将为它们调用默认规则 ( { print $0 })。因此,只保存偶数行,将输出重定向awk到一个新文件:
awk 'NR%2==0' infile > outfile
sed
sed
You can accomplish the same thing with sed. devnullsanswer shows how to do it with GNU sed.
Below are alternatives for versions of sedthat do not have the ~operator:
您可以使用sed. devnulls答案显示了如何使用GNU sed. 以下是sed没有~运算符的版本的替代方案:
keep odd lines
保持奇数行
sed 'n; d' infile > outfile
keep even lines
保持均匀的线条
sed '1d; n; d' infile > outfile
回答by devnull
Using GNU sed:
使用 GNU sed:
sed -i '0~2d' filename
to remove the even numbered lines from the file.
从文件中删除偶数行。
For removing odd numbered lines:
删除奇数行:
sed -i '1~2d' filename
The -ioption would cause the changes to be saved to the file in-place.
该-i选项将使更改就地保存到文件中。
Quoting from the manual:
引用手册:
`FIRST~STEP'
This GNU extension matches every STEPth line starting with line
FIRST. In particular, lines will be selected when there exists a
non-negative N such that the current line-number equals FIRST + (N
* STEP). Thus, to select the odd-numbered lines, one would use
`1~2'; to pick every third line starting with the second, `2~3'
would be used; to pick every fifth line starting with the tenth,
use `10~5'; and `50~0' is just an obscure way of saying `50'.
回答by twalberg
Here's an awkexample to create two new files containing the odd and even lines, respectively:
下面是awk创建两个分别包含奇数行和偶数行的新文件的示例:
awk '{ if (NR%2) print > "odd.txt"; else print > "even.txt" }' input.txt
回答by Ed Morton
Don't focus on the negative (removing lines), focus on the positive (selecting lines) and your solution will follow suit. So instead of I need to remove odd linesyou should be thinking I need to select even linesand then the solution is simply:
不要关注负面(删除线条),专注于正面(选择线条),您的解决方案将效仿。所以,而不是I need to remove odd lines你应该思考I need to select even lines,然后解决方案很简单:
awk '!(NR%2)' file
If you want to save the result to a new file:
如果要将结果保存到新文件:
awk '!(NR%2)' file > newfile
or back to the original:
或回到原来的:
awk '!(NR%2)' file > newfile && mv newfile file
回答by Chris Koknat
Perl solution for printing evens to new file:
用于将事件打印到新文件的 Perl 解决方案:
perl -lne 'print if $. % 2 == 0' infile > outfile
To print odds, change == 1to == 0
要打印赔率,请更改== 1为== 0
$.is the line number
$.是行号
Keeps only evens in the original file:
在原始文件中只保留偶数:
perl -i -lne 'print if $. % 2 == 0' infile
Same as above, but makes a backup file called infile.bak:
与上面相同,但创建一个名为 infile.bak 的备份文件:
perl -i.bak -lne 'print if $. % 2 == 0' infile
回答by potong
This might work for you (both GNU and non-GNU sed):
这可能对您有用(GNU 和非 GNU sed):
sed -n 'p;n' file # keep odd
sed -n 'n;p' file # keep even
-n: suppress printing
-n: 抑制打印
p: print current line
p: 打印当前行
n: next line
n: 下一行

