Display every nth line in a file in Linux bash

Home > Search
  by

Let's say file.txt contains the following text.

removeMe
Hello
World
removeMe
Hello
World
removeMe
Hello
World
removeMe
Hello
World

 

With the following sed command, starting at line 1 in the file, remove line 1 and also remove every third line thereafter.

~]# cat file.txt | sed '1~3d'
Hello
World
How
are
you
today?

 

In this example, starting at line 0, insert a new line at every third line.

~]# sed -i '0~3 s|$|\n|g' file.txt
removeMe
Hello
World

removeMe
Hello
World

removeMe
Hello
World

removeMe
Hello
World

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments