Often you may want to use awk to extract specific columns from a file.
You can use the following basic syntax to do so:
awk -F, '{OFS=",";print $1,$4}' nba_data.csv
This particular example extracts columns 1 and 4 from the CSV file named nba_data.csv.
You could also use the following syntax to extract specific columns and then save the results to a new file:
awk -F, '{OFS=",";print $1,$4}' nba_data.csv > nba_data_new.csv
This particular example extracts columns 1 and 4 from the CSV file named nba_data.csv and saves the results to a new CSV file named nba_data_new.csv.
Here is what the various awk commands do:
- -F, specifies that the input delimiter is a comma.
- OFS=”,” specifies that the printed output delimiter should be a comma
- $1,$4 specifies that columns 1 and 4 should be extracted from the file
The following example shows how to use this syntax in practice.
Example: How to Use awk to Extract Specific Columns from File
Suppose that we have a file named nba_data.csv that contains information about various basketball players.
We can use the cat command to view the contents of this file:

Suppose that we would like to extract columns 1 and 4 from the file.
We can use the following syntax to do so:
awk -F, '{OFS=",";print $1,$4}' nba_data.csv
The following screenshot shows how to use this syntax in practice:

Notice that this returns only columns 1 (“team” column) and 4 (“rebounds” column) from the file.
Feel free to change 1 and 4 to whatever numbers you would like depending on which columns you would like to extract from your own file.
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
