how to get every 4th line from a file?

P

papori

Guest
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
 


Extracting lines from file

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

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 :)
 
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 :)

That's help!
 

Members online


Top