Bash: Split File into Multiple Files Based on Column Value


Often you may want to use Bash to split one file into multiple files based on the values in a specific column.

You can use the following syntax to do so:

awk '{print>$1".txt"}' player_data.txt

This particular example splits the file named player_data.txt into multiple text files based on the value in column 1 of the file.

Note that the awk command print>$1 allows us to print the current line to a file whose name is the value from column 1.

Note that we also use “.txt” following the print command to specify that the new files that are created should have .txt file extensions.

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

Related: Use Bash to Split File Based on Number of Lines

Example: Use Bash to Split File into Multiple Files Based on Column Value

Suppose we have the following file named player_data.txt that contains information about various basketball players:

The first column represents the team name for each player and the second column represents the points scored by each player.

Suppose that we would like to split this file into multiple files based on the team names in the first column so that we’re left with the following three new files:

  • Mavs.txt
  • Celtics.txt
  • Kings.txt

We can use the following syntax to do so:

awk '{print>$1".txt"}' player_data.txt

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

Bash split file based on column value

Notice that we have split the original file named player_data.txt into three new files that each contain the values for players on specific teams.

Note that the original file named player_data.txt still remains in the same directory unchanged.

Note: Feel free to change “.txt” after the print command in the code to a different file extension if you would like.

Related Tutorials

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

Bash: How to Find Duplicate Lines in File
Bash: How to Check if File is Empty
Bash: How to Compare Two Files
Bash: How to Check if File Contains Specific String

Leave a Reply