how to get every 4th line from a file?

Discussion in 'Command Line' started by papori, Jan 3, 2013.

  1. papori

    papori New Member

    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Hi guys,
    I have a text file ..
    I want to get every 4th line into a new file, but I want the line count will start from the 2th line.
    For example for file with 20 lines, i want line numbers:
    2
    6
    10
    14
    18

    Any simple ideas?

    Thanks,
    Pap

  2. hackinjack

    hackinjack New Member

    Messages:
    22
    Likes Received:
    2
    Trophy Points:
    0
    Extracting lines from file

    Code:
    awk '{if (! ((FNR + 2) % 4)) { print }}' myfile > newfile
    Explanation: Parse file "myfile" - IF ((current line number in file plus 2) modulo 4) is NOT TRUE print current line and redirect output to file "newfile"

    HTH :)
  3. papori

    papori New Member

    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    That's help!

Share This Page