Shell Script - Rearrange Header and Data of different versions of Text file based on a Master File

I want to re-arrange the data (Header and Detail) in multiple pipe delimited text files, in the order specified in a reference master file and create a single output file, using shell script.

The order of the columns will be different in different input files. Data could be empty for the columns. There could be columns missing or new columns in the input files, when compared to the Reference master file. The order in the Reference master file could change in future and columns could be added to or deleted from any position.

Sample Input file A:

Name|Amount|Vehicle|City|Visits|IndicatorJack|123.22|Volkswagen|Altamonte|1|YSam|23.1|Audi||3|N|21.4|Ford|NewYork||Y

Sample Input file B:

Suffix|Name|Visits|Vehicle|State|Indicator|City|GenderMr|Jack|1|Volkswagen|NJ|Y|Hoboken|MMr|Rob|6|Buick||N|Stamford|

Reference Master File:

State|City|Vehicle|Gender|Name|Amount

Output File:

State|City|Vehicle|Gender|Name|Amount  
|Altamonte|Volkswagen||Jack|123.22  
||Audi||Sam|23.1  
|NewYork|Ford|||21.4  
NJ|Hoboken|Volkswagen|M|Jack|  
|Stamford|Buick||Rob|