LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Compare 2 columns in two files and then put matching data in a file (https://www.linuxquestions.org/questions/linux-newbie-8/compare-2-columns-in-two-files-and-then-put-matching-data-in-a-file-4175657233/)

deep_kol 07-11-2019 03:30 PM

Compare 2 columns in two files and then put matching data in a file
 
Hi Folks ,
Need your help in unix to solve this issue .

I have 2 data files a.txt (csv) and b.txt (fixed length ) . Need to search from b.txt for which the 1st 2 columns matches from both the files and the output is kept in a new file .


a.txt
------------
1,b,20
2,c,40


b.txt
------------------
1 b Apple
2 d Jack
3 c Fruits
2 c Adam
4 c Adam



output c.txt

1 b Apple
2 c Adam

Turbocapitalist 07-11-2019 03:39 PM

The join utility will do that but you'll have to standarized the files first so that the delimiter is the same for both.

BW-userx 07-11-2019 07:04 PM

I did not know about join,
Code:

#!/bin/bash

 
tempfile=$HOME/workingtemp/tempfile
save1=$HOME/workingtemp/a.txt
save2=$HOME/workingtemp/b.txt
saveA3=$HOME/workingtemp/ResultsA
saveB3=$HOME/workingtemp/ResultsB


formatListings()
{       
  sed -i 's/,/ /g' "$save1"
  join "$save1" "$save2" > "$tempfile"
  cat "$tempfile"
}
formatListings

#just kept these in , they might come in handy for you.

FindFilesNotProccesed2()
{        echo "FindFilesNotProccesed2"
        while read dirs
        do
                echo "$dirs"
                echo  "$dirs" >> "$saveB3"
                #23 what matches between two list
        done < <(comm -23 <(sort $save1) <(sort $save2))
}
FindFilesNotProccesed2


FindFilesNotProccesed1()
{        echo "FindFilesNotProccesed1"
        while read dirs
        do
                echo "$dirs"
                echo  "$dirs" >> "$saveA3"
                #13 what does not match between two list               
        done < <(comm -13 <(sort $save1) <(sort $save2))
}
FindFilesNotProccesed1

you'll should work out the rest of it. You also should see what I mean by that if you put this to work for you. ;) cheers!

grail 07-12-2019 03:06 AM

Looks like homework that should have been attempted before requesting help. Please show your efforts in future

BW-userx 07-12-2019 06:47 AM

well if it is , it is still not completely correct, its just a push in a direction that might lead him to the solution.

TB0ne 07-12-2019 06:50 AM

Quote:

Originally Posted by deep_kol (Post 6014194)
Hi Folks ,
Need your help in unix to solve this issue . I have 2 data files a.txt (csv) and b.txt (fixed length ) . Need to search from b.txt for which the 1st 2 columns matches from both the files and the output is kept in a new file .

a.txt
------------
1,b,20
2,c,40

b.txt
------------------
1 b Apple
2 d Jack
3 c Fruits
2 c Adam
4 c Adam

output c.txt

1 b Apple
2 c Adam

Wow, this seems like familiar territory, seeing as you've made similar no-effort homework-question posts in the past:
https://www.linuxquestions.org/quest...ne-4175609588/
https://www.linuxquestions.org/quest...ix-4175627245/


All times are GMT -5. The time now is 06:35 AM.