Home > Mobile >  2 Files to compare with columns
2 Files to compare with columns

Time:10-29

I have got 2 files and need an output

file 1 column 1 == file 2 column 1 > output
file 1 column 1 =! file 2 column 1 [Remove]

file 1 contains 2 columns

file 2 only 1

So if the first column from file 1 is the same as file 2 then it needs to output the 2 columns from file 1.

example file 1=

Cheese:12
Bloom:13
Kitkat:3478

File 2=

Cheese
Kitkat

Output=

Cheese:12
Kitkat:3478

CodePudding user response:

I may have misunderstood your question (the formatting needs to be improved), but perhaps this is what you're after:

file1

Cheese:12
Bloom:13
Kitkat:3478

file2

Cheese
Kitkat

Get values in file1 based on file2:

awk -F":" 'NR==FNR{a[$1]=$1; next}{if($1 in a) print}' file2 file1
Cheese:12
Kitkat:3478

CodePudding user response:

Think of it as selecting lines in file 1 from file 2, then with grep:

grep -f file2 file1

Output:

Cheese:12
Kitkat:3478
  • Related