0

I have 2 files that contain numbers_ID, status, descrpation I want to join both files based on the numbers as

number_123, status1, status2 

My file 1:

number_123,this car is under maintenance  
number_345,this car checked is done
number_356,this car is under main 

My file 2:

number_123,hold  
number_345,done 

I need to join only the existing number in both files as :

number_123,hold,this car is under maintenance    
number_345,done,this car checked is done 

I used comm file1 file2 to find the common numbers but the file look like:

number_123,this car is under maintenance 
number_123,hold 
number_345,this car checked is done
number_345,done

How Can I print it in one line as

 number_123,hold,this car is under maintenance    
 number_345,done,this car checked is done 

migrated from unix.meta.stackexchange.com Aug 2 '18 at 16:54

This question came from our discussion, support, and feature requests site for users of Linux, FreeBSD and other Un*x-like operating systems.

1

The comm utility is used to compare whole lines between files. What you want to do is to join on a particular field.

$ join -t, file2 file1
number_123,hold,this car is under maintenance
number_345,done,this car checked is done

This assumes that both files are sorted on the join field (the first comma-delimited column in each file).

If the files are not sorted, you may pre-sort them using

sort -t, -k1,1 -o file1 file1
sort -t, -k1,1 -o file2 file2

In ksh93, bash or zsh, you may also do the sort "on the fly":

join -t, <( sort -t, -k1,1 file2 ) <( sort -t, -k1,1 file1 )
  • Thank you very much for the explanation and help, -join cmd is more suit to my problem than comm. – Lona gracia Aug 3 '18 at 10:57

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.