1

I have two files file1.csv and file2.csv.

Below are the contents of file1.csv

AL.jar;d8c06ebedd7954681f34ab5c94fdc4fb
AR.jar;9a553dd203d0979aa60004e19cc98c12
BI.jar;8022f6c5f83ba040394ff0b0a0323e8e
BV.jar;f53c4a8c988aa8806b54063ebc682803
CaseUtilities.jar;e5f653d899298f5e5d56f357b6f781c5
CO.jar;b2f7a0ab6e646d6793631e5c97e05096

file2.csv

AL.jar;d8c06ebedd7954681f34ab5c94fdc4fb
AR.jar;4e6e584dd852684ba21ae63990e2a1a6
BV.jar;213d9df82095764702ef4929424a1a0c
CaseUtilities.jar;5b787f1f3d57922bd980ebbfe9a5343e
CO.jar;cfb994078ff4373c7e0f15de19830a3d
Common.jar;a09b520288870aa3888194ce59179dbd

We need to compare two files based on the contents.

I want to make diff which is based only on values of first column, so the result should be

AL.jar;d8c06ebedd7954681f34ab5c94fdc4fb                         AL.jar;d8c06ebedd7954681f34ab5c94fdc4fb
AR.jar;9a553dd203d0979aa60004e19cc98c12                         AR.jar;4e6e584dd852684ba21ae63990e2a1a6
BI.jar;8022f6c5f83ba040394ff0b0a0323e8e                         <NULL>
BV.jar;f53c4a8c988aa8806b54063ebc682803                        BV.jar;213d9df82095764702ef4929424a1a0c
CaseUtilities.jar;e5f653d899298f5e5d56f357b6f781c5              CaseUtilities.jar;5b787f1f3d57922bd980ebbfe9a5343e
CO.jar;b2f7a0ab6e646d6793631e5c97e05096                         CO.jar;cfb994078ff4373c7e0f15de19830a3d
<NULL>                                                          Common.jar;a09b520288870aa3888194ce59179dbd

I have tried the below command

diff -y file1.csv file2.csv

But the below output is not as expected.

AL.jar;d8c06ebedd7954681f34ab5c94fdc4fb                         AL.jar;d8c06ebedd7954681f34ab5c94fdc4fb
AR.jar;9a553dd203d0979aa60004e19cc98c12                       | AR.jar;4e6e584dd852684ba21ae63990e2a1a6
BI.jar;8022f6c5f83ba040394ff0b0a0323e8e                       | BV.jar;213d9df82095764702ef4929424a1a0c
BV.jar;f53c4a8c988aa8806b54063ebc682803                       | CaseUtilities.jar;5b787f1f3d57922bd980ebbfe9a5343e
CaseUtilities.jar;e5f653d899298f5e5d56f357b6f781c5            | CO.jar;cfb994078ff4373c7e0f15de19830a3d
CO.jar;b2f7a0ab6e646d6793631e5c97e05096                       | Common.jar;a09b520288870aa3888194ce59179dbd

Any idea how can my expected output be achieved!

0

You could always do it in awk:

$ awk -F';' '{ 
                if(NR==FNR){a[$1]=$0}
                else{b[$1]=$0} 
                ids[$1]++; 
             } 
             END{
                    for(id in ids){
                        printf "%s\t%s\n",a[id],b[id];
                    }
            }' file1.csv file2.csv | column -t
CO.jar;b2f7a0ab6e646d6793631e5c97e05096             CO.jar;cfb994078ff4373c7e0f15de19830a3d
BV.jar;f53c4a8c988aa8806b54063ebc682803             BV.jar;213d9df82095764702ef4929424a1a0c
Common.jar;a09b520288870aa3888194ce59179dbd         
AL.jar;d8c06ebedd7954681f34ab5c94fdc4fb             AL.jar;d8c06ebedd7954681f34ab5c94fdc4fb
AR.jar;9a553dd203d0979aa60004e19cc98c12             AR.jar;4e6e584dd852684ba21ae63990e2a1a6
BI.jar;8022f6c5f83ba040394ff0b0a0323e8e             
CaseUtilities.jar;e5f653d899298f5e5d56f357b6f781c5  CaseUtilities.jar;5b787f1f3d57922bd980ebbfe9a5343e

Or, to include the NULL from your example output:

$ awk -F';' '{ 
                if(NR==FNR){
                    a[$1]=$0;
                    b[$1]="<NULL>"
                }
                else{
                    b[$1]=$0;
                    a[$1] = a[$1] ? a[$1] : "<NULL>";
                } 
                ids[$1]++; 
             } 
             END{
                    for(id in ids){
                        printf "%s\t%s\n",a[id],b[id];
                    }
            }' file1.csv file2.csv | column -t
CO.jar;b2f7a0ab6e646d6793631e5c97e05096             CO.jar;cfb994078ff4373c7e0f15de19830a3d
BV.jar;f53c4a8c988aa8806b54063ebc682803             BV.jar;213d9df82095764702ef4929424a1a0c
<NULL>                                                Common.jar;a09b520288870aa3888194ce59179dbd
AL.jar;d8c06ebedd7954681f34ab5c94fdc4fb             AL.jar;d8c06ebedd7954681f34ab5c94fdc4fb
AR.jar;9a553dd203d0979aa60004e19cc98c12             AR.jar;4e6e584dd852684ba21ae63990e2a1a6
BI.jar;8022f6c5f83ba040394ff0b0a0323e8e             <NULL>
CaseUtilities.jar;e5f653d899298f5e5d56f357b6f781c5  CaseUtilities.jar;5b787f1f3d57922bd980ebbfe9a5343e
0
awk -F "\"*;\"*" '{print $1}' file1.csv > file1   # get first column from file1.csv with awk as stream and redirect to file1 var
awk -F "\"*;\"*" '{print $1}' file2.csv > file2   # get first column from file2.csv with awk as stream and redirect to file2 var
diff -y file1 file2   # diff file1 and file2 bash vars

Or same with single command:

diff -y <(awk -F "\"*;\"*" '{print $1}' file1.csv)  <(awk -F "\"*;\"*" '{print $1}' file2.csv)

Result:

AL.jar                              AL.jar
AR.jar                              AR.jar
BI.jar                                <
BV.jar                              BV.jar
CaseUtilities.jar                       CaseUtilities.jar
CO.jar                              CO.jar
                                  | Common.jar
  • It would be good if you explain?? – Prvt_Yadv Dec 12 '18 at 14:00
  • Added explanations to first example. Second example is just compact mode of first. – mature Dec 12 '18 at 14:06

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.