-1

I have two csv files with similar content/data.


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

Please let me know the command used for this type of query.

5
  • 2
    What kind of query are you trying to do? Right now we just have 2 input files from you. – Eric Renouf Nov 18 '16 at 13:15
  • 1
    see also: unix.stackexchange.com/questions/134829/… – Sundeep Nov 18 '16 at 13:22
  • @EricRenouf i have given two input files, so a query/command/sed/awk/grep anything should comapre these two inout files and get a data which is similar like the one i gave in output – Siddharth Sahoo Nov 18 '16 at 13:30
  • Yes, but how should the output be generated from those two files? Explain in words. Do you just want the 1,2,4,5,and 7th lines from file? Are you looking for grep -Ff file1 file2? What if file2 contains a ADISBADIS, should that match ADIS? You need to tell us what you need instead of expecting us to guess. – terdon Nov 21 '16 at 14:55
  • file1.csv contains 118 rows , file2.csv contains 2500 rows with 2 columns My requirement is the code/script should match both the csv files and fetch the matched row in a new file. I even tried grep -Ff file1 file2 but no use, there will be only single string with the same in both files. I am running GNU/Linux – Siddharth Sahoo Nov 21 '16 at 15:02
0

Using grep;

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

using awk

awk -F, 'NR==FNR{seen[$0]++;next} ($1 in seen)' file1.csv file2.csv > outfile.csv
5
  • this doesnt work, i have already gave a try – Siddharth Sahoo Nov 18 '16 at 13:27
  • I have tested on your input files, output is same as your desired one!! – αғsнιη Nov 18 '16 at 13:32
  • i have tried with awk also , it gives an empty file – Siddharth Sahoo Nov 18 '16 at 14:24
  • Hi Any other work-around coz both options didn't work – Siddharth Sahoo Nov 21 '16 at 10:16
  • Could you please double check if the code is right, coz i am not getting any output from it – Siddharth Sahoo Nov 21 '16 at 12:03
0

Using Bash process substitution, you can sort the files and then "join" them.

join -t, <(sort file1.csv) <(sort file2.csv) > output.csv
-1

You can solve this with a simple script to execute in the shell. Try with this:

while read i; do grep $i ./file2.csv >> output.csv; done < file1.csv

It will create a new file called output.csv with the result that you are expecting for. You need to run it on the folder where you have stored both files file1.csv and file2.csv.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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