Skip to content
Advertisement

How to compare the columns of file1 to the columns of file2, select matching values, and output to new file using grep or unix commands

I have two files, file1 and file2, where the target_id compose the first column in both.

I want to compare file1 to file2, and only keep the rows of file1 which match the target_id in file2.

file1

file2:

JavaScript

Any help would be appreciated.

% grep -x -f file1 file2 resulted in no output in my terminal

Advertisement

Answer

Sample data that actually shows overlaps between the files.

  • file1.csv:

    JavaScript
  • file2.csv

    JavaScript

Your grep command, but swapped:

JavaScript

Edit: we can add the -F argument since it is a fixed-string search. Plus it adds protection against the . matching something else as a regex. Thanks to @Sundeep for the recommendation.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement