How to Use awk to Print All Fields Except First Field


Often you may want to use awk to print all fields except the first field in a file.

You can use the following syntax to do so:

awk '{for (i=2; i<NF; i++) printf $i " "; print $NF}' player_stats.txt

This particular example prints all fields except for the first field of the text file named player_stats.txt.

Note that this syntax uses a for loop to print each column in the file ranging from column 2 to column NF, which stands for “number of fields” and extracts the last field in a file.

By using this approach, we’re able to print all fields except the first field in a file.

The following example shows how to use this syntax in practice.

Example: Use awk to Print All Fields Except First Field

Suppose we have the following text file named player_stats.txt that contains information about statistics for various basketball players:

Suppose that we would like to print all fields from this file except for the first (“team”) field.

We can use the following syntax to do so:

awk '{for (i=2; i<NF; i++) printf $i " "; print $NF}' player_stats.txt

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

awk print all fields except first

Notice that this prints all fields in the file except for the first field.

Related Tutorials

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

How to Use awk to Replace newline with Space
How to Use awk to Replace All Occurrences of String in File
How to Use awk to Extract Substring

Leave a Reply