Bash: Delete Lines from One File that Are in Another File


Often you may want to use Bash to delete all lines in one file that are in another file.

You can use the following syntax with awk to do so:

awk '{if (f==1) { r[$0] } else if (! ($0 in r)) { print $0 } } ' \
f=1 good_teams.txt f=2 all_teams.txt

This particular example will delete all lines from the all_teams.txt file that are also in the good_teams.txt file.

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

Example: Use Bash to Delete Lines from One File that Are in Another File

Suppose that we have a file named all_teams.txt that contains a long list of basketball team names.

Suppose we have another file named good_teams.txt that only contains a few names of the “good” basketball teams.

We can use the cat command to view the contents of each of these files:

Suppose that we would like to delete all lines from the all_teams.txt file that are also in the good_teams.txt file.

We can use the following syntax to do so:

awk '{if (f==1) { r[$0] } else if (! ($0 in r)) { print $0 } } ' \
f=1 good_teams.txt f=2 all_teams.txt

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

Bash delete all lines in file that are in another file

This returns each of the team names from the all_teams.txt file with the following team names from the good_teams.txt file removed:

  • Houston Rockets
  • Denver Nuggets
  • Miami Heat

Feel free to use similar syntax to remove all lines from your own file that exist in another specific file.

Related Tutorials

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

Bash: How to List All Files in Folder by Size
Bash: How to List All Files in Folder Without Extension
Bash: How to List All Files Matching a Specific Pattern

Leave a Reply