How to remove the first line of a text file using the linux command line?

Use the Linux command line tool to remove the first line of the text file, which we may also use in daily development.

This article will share with you how to delete the first line of a text file using the linux command line tool.

The following is the content of the test text file used in this article.

This is a test file.
The sed command can use the action option d to remove any line of the file.
The awk command can use the NR variable to remove any line in the file.
The tail command can remove consecutive lines of the file.

The linux sed command can easily remove any specified line of the file, so here we can use the sed command to remove the first line of the text file.

Use the sed command to remove the first line of the text file

In the following example, we will use the sed command to remove the first line of the text file.

➜  ~ sed '1d' test.txt

Use the awk command to remove the first line of the text file

In the following example, we will use the NR variable of the awk command to determine whether the NR variable is the first line of the file.

If the variable NR is equal to 1, it means that the first line of the file does not output the content, otherwise the content is output.

➜  ~ awk '{if(NR > 1) {print $0}}' test.txt

Use the tail command to remove the first line of the text file

In the following example, we will use the tail command -n option to remove the first line of the file.

tail -n x just print the last x lines. tail -n 3 would give you the last 3 lines of the input. The + sign kind of inverts the argument and make tail print anything but the first x-1 lines. tail -n +1 would print the whole file, tail -n +2 everything but the first line, etc.

➜  ~ tail -n +2 test.txt

Add a Comment

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