Remove every other line in a file in Linux bash

Home > Search
  by

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

Hello
Hello
World
World
How
How
are
are
you?
you?

 


Delete a specific line

The following command will remove a specific line. In this example, line 5 is removed.

~]# sed -i 5d /path/to/file.txt

 

The result of the command will be:

Hello
Hello
World
World
How
are
are
you?
you?

 

 


Remove every other line

The following sed command will remove every other line of text from the file.

~]# sed -i '1~3d' /path/to/file.txt

 

The result of the command will be:

Hello
World
How
are
you
today?

 



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