0

File 1 (Master Data):

12345|abc  
11223|xyz  
23234|pqr

File 2 (Sub set of File1):

12345_ASDD|PASD|AWOP  
11223_PLDD|EVAAA ASDAS|ASDD  
23234_MJKJLO|OKEI JSN|OPIE  

Output:

12345_ASDD|PASD|AWOP|abc  
11223_PLDD|EVAAA ASDAS|ASDD|xyz  
23234_MJKJLO|OKEI JSN|OPIE|pqr

Explanation:
First column (before underscore) of File2 should match with first column of File1 and corresponding second column of File1 should be added to File2.

3
  • What have you tried so far? Is sort order important or can this be changed during the process? Are all keys of File 1 in File 2 (and vice versa), or does one of the files have less lines than the other? – nohillside Nov 5 '18 at 7:52
  • Sort order is not important until data is as expected.. All keys of File2 are in File1.. File2 is sub set of File1.. File2 have lesser lines from File1.. – Maddys Nov 5 '18 at 7:56
  • awk 'BEGIN{FS=OFS="|"}NR==FNR{a[$0];next}{for(i in a)if(index(i,$2)==1) print i,$1}' file1 file2... Tried this so far.. not able to get the desired output.. – Maddys Nov 5 '18 at 7:57
3

Probably the simplest approach is to change the FS before the second file - then for example you can do the familiar field-based lookup

awk 'BEGIN{OFS=FS="|"} NR==FNR {a[$1]=$2; next} {print $0,a[$1]}' File1 FS="_" File2

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.