0

How can I read 2 CSV files (data comma seperated) as input using shell script and do operation on that?

File1.csv:

ENG_IT,P1234,Ajay,India,ajay.com
ENG_GN,A4324,Raj,England,raj.com

File2.csv:

P1234,ajay.com
A4324,raj.com

Logic: Read 1st column of File1.csv and last column of File2.csv based on common column in both files i.e. (P1234, A4324) and save them as variable like var1=ENG_IT var2=ajay.com

Thanks

1
  • Isn't the information already in file1.csv (ajay.com and raj.com)? Commented Jun 25, 2013 at 8:21

1 Answer 1

1

Not sure what you want to do with the variables, but you can join the two files and then read it in a loop as shown below:

join -t, -1 2 -2 1 <(sort -t, -k2 file1.csv) <(sort file2.csv) | while IFS=, read -r -a arr
do
    var1=${arr[0]}
    var2=${arr[5]}
    echo "$var1 $var2"
done

You can also use awk to print out fields:

join -t, -1 2 -2 1 <(sort -t, -k2 file1.csv) <(sort file2.csv) | awk -F, '{print $1,$5}'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.