-1

I have two .csv files namely file1.csv and file2.csv

file1.csv

ADIS
BAP3
Mercury_System
nxh-2003
DR_FeatureUP_PT

file2.csv

ADIS,projects.adis
EcoSystems,projects.ecosystems
em1xxxsw,projects.em1xxxsw
BAP3,projects.bap3
Dirana4,projects.dirana4
Mercury_System,projects.mercury_system
nxh-2003,projects.nxh-2003
DocStore,projects.docstore
DR_FeatureUP_PT,projects.dr_featureup_pt

Desired output.csv

ADIS,projects.adis
BAP3,projects.bap3
Mercury_System,projects.mercury_system
nxh-2003,projects.nxh-2003
DR_FeatureUP_PT,projects.dr_featureup_pt

I have already tried couple of codes below, but none of them worked for me as per the requirement

grep -Ff file1.csv file2.csv > outfile.csv

awk -F, 'NR==FNR{seen[$0]++;next} ($1 in seen)' file1.csv file2.csv > outfile.csv

file1.csv contains 2500 rows and file2.csv contains 118 rows, so it should compare and give me only results which are matching to file2, output should be matching to 118 rows/results.

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227

1 Answers1

0

The following grep should return the desired results assuming file1.csv has only one column for each row. This uses each line in file1.csv as a search string (needle) and searches through file2.csv (haystack).

grep -f file1.csv file2.csv | tee outfile.csv

I added tee so you can see the output as well as writing it to a file. Your question is very vague as to what problem you are experiencing. I've done this many times on RHEL and Debian and tested just now with your sample contents. I was able to achieve your desired results.

smokes2345
  • 845
  • 4
  • 18