bash remove lines from file that are in another file code example
Example 1: awk how to remove lines in one file that are found in another file
# Example 1
awk 'NR==FNR{a[$0];next} !($0 in a)' file_2 file_1
# This returns all lines in file_1 that are not found in file_2
# See source for more info on how the command works
Example 2: bash print lines unique to one file
# Example usage using awk:
awk 'FNR==NR { b[$0] = 1; next } !b[$0]' input_file_1 input_file_2
# This command prints all lines of input_file_2 that aren't found in
# input_file_1 (i.e. lines that are unique to input_file_2). Another
# way of saying this is that this removes all lines of input_file_2
# that are found in input_file_1.
# It does this by first adding all unique lines of input_file_1 to an
# associative array, which works like a dictionary with a key value
# pair. Then each line of input_file_2 is compared against the array
# and if it isn't found in the array, it is printed.
# Note, change $0 to $# if you want to remove lines based on values in
# specific fields (which don't have to be the same in each file)
# Note, unlike some comm and diff commands, this solution doesn't require
# the files to be sorted.