1

Can someone please help me with below. I have two different files and I need to join the files but with some condition.

File1

ABC,10,20,3
GHI,8,6,2

File2

ABC,5000
DEF,6000
GHI,1000

Required output is as below

All columns of file1 + 2nd column of file2 where 1st column of file 1 matches with 1st column of file 2

Output expected

ABC,10,20,3,5000
GHI,8,6,2,1000
1
  • 1
    What have you tried? since your first column appears to be lexically sorted, it should be trivial using the join command Commented Oct 19, 2019 at 12:56

1 Answer 1

1
join -t',' -1 1 -2 1 -o 1.1,1.2,1.3,1.4,2.2 <(sort FILE1) <(sort FILE2)

ABC,10,20,3,5000
GHI,8,6,2,1000
  • Field separator (t) = ','
  • join -1 1 = FILE1 column 1, -2 1 = FILE2 column 1

  • -o = output, 1.1 = FILE1.column 1, ...

1
  • Or for the lazy ones: join -t',' <(sort FILE1) <(sort FILE2) Commented Oct 19, 2019 at 17:06

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.