-1

This question already has an answer here:

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.

marked as duplicate by l0b0, GAD3R, countermode, techraf, Anthony Geoghegan Nov 21 '16 at 14:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • its not a duplicate of the question mentioned as this is a csv file that i have asked and the one you mentioned is piped file. – Siddharth Sahoo Nov 21 '16 at 12:45
  • Even that didn't have a solution – Siddharth Sahoo Nov 21 '16 at 12:45
  • There is no fundamental difference between the contents of a file and piped contents, as long as they are both finite. – l0b0 Nov 21 '16 at 12:48
  • 2
    Cannot reproduce. When I do grep -Ff file1 file2 I get your desired output (using grep in macOS). – maulinglawns Nov 21 '16 at 12:51
  • @maulinglawns i have tried it, but i am on RHEL. It's not working here, even with awk i get a blank output – Siddharth Sahoo Nov 21 '16 at 12:56
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.

  • i have already tried this option but no use, now i tried on GNU/Linux but still nothing i get – Siddharth Sahoo Nov 21 '16 at 14:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.