[英]Remove odd or even lines from a text file
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.这只会在终端中打印它们。
The %
is a modulus operator and NR
is the current line number, so NR%2==0
is true only for even lines and will invoke the default rule for them ( { print $0 }
). %
是模数运算符, NR
是当前行号,因此NR%2==0
仅对偶数行为真,并将为它们调用默认规则( { print $0 }
)。 Thus to save only the even lines , redirect the output from awk
to a new file:因此,只保存偶数行,将输出从awk
重定向到一个新文件:
awk 'NR%2==0' infile > outfile
You can accomplish the same thing with sed
.你可以用sed
完成同样的事情。 devnulls answer shows how to do it with GNU sed
. devnulls答案显示了如何使用GNU sed
做到这一点。 Below are alternatives for versions of sed
that do not have the ~
operator:以下是没有~
运算符的sed
版本的替代方案:
keep odd lines保持奇数行
sed 'n; d' infile > outfile
keep even lines保持均匀的线条
sed '1d; n; d' infile > outfile
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 -i
option 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'.
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
: 下一行
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 lines
you should be thinking I need to select even lines
and then the solution is simply:因此,您应该认为I need to select even lines
而不是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
这是一个awk
示例,用于创建分别包含奇数行和偶数行的两个新文件:
awk '{ if (NR%2) print > "odd.txt"; else print > "even.txt" }' input.txt
Perl solution for printing evens to new file:用于将事件打印到新文件的 Perl 解决方案:
perl -lne 'print if $. % 2 == 0' infile > outfile
To print odds, change == 1
to == 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.