Often you may want to use Bash to remove specific columns from a file.
You can use the following syntax with cut to do so:
cat player_stats.txt | tr -s ' ' | cut -d ' ' --complement -f2
This particular example will remove column 2 from the text file named player_stats.txt.
You can also specify multiple column numbers to remove from the file:
cat player_stats.txt | tr -s ' ' | cut -d ' ' --complement -f2,4
This particular example will remove columns 2 and 4 from the text file named player_stats.txt.
Here is how this syntax works:
- We use cat to display all lines in the file
- We use tr -s ‘ ‘ to convert any repeated spaces into a single space
- We use cut -d ‘ ‘ –complement to print all columns except specific columns
By using this syntax, we are able to remove specific columns from a file.
The following example shows how to use this syntax in practice.
Example: Use Bash to Remove Specific Columns from a File
Suppose that we have a file named player_stats.txt 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 remove column 2 (the “points” column) from the file.
We can use the following syntax to do so:
cat player_stats.txt | tr -s ' ' | cut -d ' ' --complement -f2
The following screenshot shows how to use this syntax in practice:

Notice that column 2 has been deleted from the file.
We could also use the following syntax to delete both column 2 and 4 from the file:
cat player_stats.txt | tr -s ' ' | cut -d ' ' --complement -f2,4
The following screenshot shows how to use this syntax in practice:

Notice that columns 2 and 4 have both been deleted from the file.
Related Tutorials
The following tutorials explain how to perform other common tasks in Bash:
Bash: How to Swap Two Columns in a File
Bash: How to Delete Duplicate Lines in File
Bash: How to Replace All Occurrences of String in File
Bash: How to Replace Specific Line in File
