Bash (Scripting) - do something every other match

by
Jeremy Canfield |
Updated: March 08 2020
| Bash (Scripting) articles
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?
Did you find this article helpful?
If so, consider buying me a coffee over at