Issue
I have two files where I want to compare the first column to find the unique ones using unix shell.
File 1:
yellow
green
red
white
File 2:
yellow,not_started
red,in_progress
I want the output to be
green
white
Solution
Easy to do with join
:
$ join -t, -v1 <(sort file1.txt) <(sort file2.txt)
green
white
-v1
tells it to print out records in the first file that don't match up to a record in the second file. The sorts are because join
requires its input files to be sorted based on the (default first) join column.
Alternative using awk
:
$ awk -F, 'NR == FNR { seen[$1] = 1; next } !($0 in seen)' file2.txt file1.txt
green
white
Note the second file comes first in the arguments to awk
.
Answered By - Shawn