Friday, April 18, 2008
11:09 AM

Use sed or perl to extract every nth line in a text file

I recently blogged about the use of sed to extract lines in a text file.

As examples, I showed some simple cases of using sed to extract a single line, and a block of lines in a file.

An annoymous reader asked how one would extract every nth line from a large file.

Suppose somefile contains the following lines:
$ cat > somefile
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
$


Below, I show 2 ways to extract every 4th line: lines 4 and lines 8 in somefile.

  1. sed
    $ sed -n '0~4p' somefile
    line 4
    line 8
    $

    0~4 means select every 4th line, beginning at line 0.

    Line 0 has nothing, so the first printed line is line 4.

    -n means only explicitly printed lines are included in output.

  2. perl
    $ perl -ne 'print ((0 == $. % 4) ? $_ : "")'  somefile
    line 4
    line 8
    $


    $. is the current input line number.

    % is the remainder operator.

    $_ is the current line.


    The above perl statement prints out a line if its line number
    can be evenly divided by 4 (remainder = 0).

    Alternatively,
    $ perl -ne 'print unless (0 != $. % 4)' somefile
    line 4
    line 8
    $



StumbleUpon Toolbar

0 comments:

Post a Comment