mappin strings of two different file and finding the mapped string and then map other fields.

As i am new to unix so facing some problems in scripting:
here is my question:
i m having two files. 1st file say a.txt contain 3 column like

SPECIALITY|UMP_CODE|SPECIALTY_CODE
Addictive Diseases|25ADD|ADD
Addictive Diseases/Family Practice|25ADD|ADD/FP
Aerospace Medicine|1.041666667|AM
Aerospace Medicine/Family Practice|1.041666667|AM/FP
Allergy & Immunology|01AI|AI
Allergy & Immunology/Pulmonary Medicine|01AI|AI/PUL
Anatomic Pathology & Laboratory Medicine|17APL|APL
Cardiology|03C|C
Cardiology/Internal Medicine|03C|C/

and 2nd file let say b.txt contain only one column speciality:

Speciality
Abdominal
ADDICTION MEDICINE
ALLERGY
Adolescent Medicine
Family Practice
General Practice

so i want to map b.txt file column, speciality with the a.txt file and after mapping of the strings i want to get the UMP_CODE of the respected feilds also which mapped in both column. i want to assign variables name( a = &Family Practice) so that i change the fields as it is changing in every file.
my output is like this:

SPECIALITY UMP_CODE
    Aerospace Medicine 1.041666667
    Addictive Diseases 03C
    Cardiology 03C
    Family Practice 07FP
    Allergy & Immunology 01A

So thanks for any kind of help.:slight_smile:

Try:

awk -F\| 'BEGIN{print "SPECIALITY UMP_CODE"}NR>1 && NR==FNR{a[$0];next}$1 in a{print $1,$2}' b.txt a.txt

sorry to say but the code is not working it is showing the error:
can't open the source file "|" for reading.

What operating system are you using?

i m using unix operating system( bash)

---------- Post updated at 11:12 AM ---------- Previous update was at 10:46 AM ----------

hey its ok i made it work by myself.....
my code is like this

awk ' BEGIN { FS="|";
while ( getline < "all_spec.txt" > 0)
   a[$1]=$2
}
{
if($1 in a)
    printf("%s %s\n", $1, a[$1])
} ' sample.txt

but as i said you earlier i am new to UNIX so having a lot of questions in my mind,
and lot of doubts. one of them is like as in normal programming we declare variables
and later use those variables in our function and loops,in the same way i want to declare variable and can use it inside the script.
Like i want to assign my b.txt fields as variable as a= General Practice, b= Family practice
and later can use in the script. Is it possible if it is then help me on
this program how to do this by declaring variables in the above codes.
so rply as soon as possible..................