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? Commented Nov 5, 2018 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.. Commented Nov 5, 2018 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.. Commented Nov 5, 2018 at 7:57

1 Answer 1

2

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 

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.