Extract every Nth line from a Text File using Command Line

In this video, I use the command line tool awk to extract every third line from a captions.sbv file. This should work on Mac, Linux and Windows Subsystem for Linux.

Move to Desktop Folder
cd ~/Desktop
View File (hit q to exit)
less captions.sbv
Extract Every Third Line from captions.sbv
awk 'NR % 3 == 2' captions.sbv

‘3’ is for every third line.
‘2’ means start with the 2nd line.

Save Every Third Line to a Text File
awk 'NR % 3 == 2' captions.sbv > captions.txt
Convert Linefeeds to Spaces
awk 'NR % 3 == 2' captions.sbv |tr '\n' ' ' > captions.txt

Leave a comment

Your email address will not be published. Required fields are marked *