开发者

choosing even-numbered rows from a file

How do I choose even numbered rows o开发者_JAVA百科f a file? I wish to select rows #2, 4, 6, etc. from a file that contains data. Can anyone help me with this?


sed -n 'n;p' filename


An awk example (as an alternative to Mark's sed example):

awk 'NR % 2 == 0' filename

Example:

$ cat filename
line 0
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8

$ awk 'NR % 2 == 0' filename
line 1
line 3
line 5
line 7


With awk:

awk '{getline;print}' filename

With bash:

#!/bin/bash
while read line
do
    read line
    echo $line
done < filename


If your sed is GNU's:

sed -n 2~2p filename
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜