How to Skip First Line in awk (With Examples)


Often you may want to use skip the first line when using awk to perform various commands on a file.

The easiest way to do so is by using tail -n + 2 with whatever awk command you’d like to perform, which explicitly tells awk to ignore the first line of the file.

The following examples show how to use this syntax in practice with the following file named players.txt that contains information about various basketball players:

Example 1: Use awk to Skip First Line when Printing Values

Suppose we use the following syntax to print all values from the first column of the file:

awk '{print $1}' players.txt

The following screenshot shows how to use this syntax in practice:

Notice how the value from the first line (the header row) is printed by default.

We could use tail -n + 2 to skip this first line when printing the values from the first column:

awk '{print $1}' players.txt | tail -n +2

The following screenshot shows how to use this syntax in practice:

awk skip first line when printing

Notice that the first line is no longer printed.

Example 2: Use awk to Skip First Line when Counting Unique Values

Suppose we use the following syntax to count the number of occurrences of each unique value in the first column of the file:

awk '{print $1}' players.txt | sort | uniq -c

The following screenshot shows how to use this syntax in practice:

The results display the number of occurrences of each unique value in the column, even for the header row value of team.

We could use tail -n + 2 to skip this first line when counting the occurrences of each value in the first column:

awk '{print $1}' players.txt | tail -n +2 | sort | uniq -c

The following screenshot shows how to use this syntax in practice:

awk skip first line when counting

Notice that the first line is now ignored.

Note: In this tutorial we illustrated two different scenarios of how to ignore the first line with awk, but you can use tail -n + 2 in a variety of different scenarios to ignore the first line when performing commands.

Related Tutorials

The following tutorials explain how to perform other common tasks in awk:

How to Use awk to Print All Columns After Specific Number
How to Use awk to Print Rows Where Column Equals Value
How to Use awk to Print Last Line of a File
How to Use awk to Print a Range of Columns

Leave a Reply