Code:
#set -x
nawk '{
split($0,arr1,"|")
ENTITY_TYPE = arr1[2]
if (ENTITY_TYPE ~ "O"){
ENTITY_TYPE = "B"
}
else {
ENTITY_TYPE = "P"
}
CUSTOMER_ID = arr1[1]
#Branch and Account Numbers should be left blank
BRANCH_NUMBER = ""
ACCOUNT_NUMBER = ""
split(arr1[3],arr2,"-")
ACCOUNT_DATE_OPEN = sprint arr2[1]arr2[2]arr2[3]
CORPORATE_NAME = arr1[4]
LAST_NAME = arr1[5]
FIRST_NAME = arr1[6]
MIDDLE_NAME = arr1[7]
NAME_SUFFIX = arr1[8]
# Extracting person gender information
PERSON_GENDER = arr1[9]
# If gender is anything other than M or F,replace it with blank
if (PERSON_GENDER ~ "F" || PERSON_GENDER ~ "M") {
}
else {
PERSON_GENDER=""
}
split(arr1[10],arr3,"-")
BIRTH_DATE = sprint arr3[1]arr3[2]arr3[3]
#AGE should be left blank
AGE = ""
# Extracting citizenship code information
CITIZEN_COUNTRY_NAME = arr1[11]
if (CITIZEN_COUNTRY_NAME ~ "US" || CITIZEN_COUNTRY_NAME ~ "UNITED STATES" || CITIZEN_COUNTRY_NAME ~ "UNITED STATES OF AMERICA" || CITIZEN_COUNTRY_NAME ~ "USA") {
CITIZEN_COUNTRY_NAME = "USA"
#FED_ID = `print arr1[12] | sed -e 's/[^0-9]//g'`
FED_ID = arr1[12]
}
else {
CITIZENSHIP_CODE = arr1[11]
FED_ID = ""
}
if (ENTITY_TYPE ~ "P") {
FED_ID_TYPE = "S"
}
else {
FED_ID_TYPE = "T"
}
#Extracting National ID information
ID_INFORMATION_1 = arr1[13]
ID_INFORMATION_2 = arr1[14]
if (length(ID_INFORMATION_1) > 0 && ID_INFORMATION_1 != "") {
NATIONAL_ID = ID_INFORMATION_1
#NATIONAL_ID = `print NATIONAL_ID | sed 's/[^0-9a-zA-Z]//g'`
NATIONAL_ID_TYPE = "DL"
}
else if (length(ID_INFORMATION_2) > 0 && ID_INFORMATION_2 != "") {
NATIONAL_ID = D_INFORMATION_2
#NATIONAL_ID = `print NATIONAL_ID | sed 's/[^0-9a-zA-Z]//g'`
NATIONAL_ID_TYPE = "DL"
}
else
NATIONAL_ID = ""
NATIONAL_ID_TYPE = ""
}
#Extracting street address information
ADDRESS_1 = arr1[15]
ADDRESS_2 = arr1[16]
STREET_ADDRESS = sprint ADDRESS_1 ADDRESS_2
STREET_ADDRESS = substr(STREET_ADDRESS,1,60)
#Extracting city information
ADDRESS_3 = arr1[17]
CITY_NAME = ADDRESS_3
#Extracting country information
COUNTRY = arr1[20]
ADDRESS_4 = arr1[18]
COUNTRY_NAME = COUNTRY
if (COUNTRY_NAME ~ "US" || COUNTRY_NAME ~ "USA" || COUNTRY_NAME ~ "UNITED STATES" || COUNTRY_NAME ~ "UNITED STATES OF AMERICA") {
COUNTRY_CODE = "USA"
}
else {
COUNTRY_CODE = substr(COUNTRY,1,3)
}
if (COUNTRY_CODE ~ "USA") {
STATE_CODE = ADDRESS_4
POSTCODE = substr(arr1[19],1,5)
FOREIGN_PROVINCE = ""
FOREIGN_POSTAL_CODE = ""
}
else {
STATE_CODE = ""
POSTCODE = ""
FOREIGN_PROVINCE = ADDRESS_4
FOREIGN_POSTAL_CODE = arr1[19]
}
printf("%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s\n", CDCI, ENTITY_TYPE, CUSTOMER_ID, BRANCH_NUMBER, ACCOUNT_NUMBER, ACCOUNT_DATE_OPEN, CORPORATE_NAME, LAST_NAME, FIRST_NAME, MIDDLE_NAME, NAME_SUFFIX, PERSON_GENDER, BIRTH_DATE, AGE, CITIZENSHIP_CODE, FED_ID, FED_ID_TYPE, NATIONAL_ID, NATIONAL_ID_TYPE, STREET_ADDRESS, CITY_NAME, STATE_CODE, POSTCODE, FOREIGN_PROVINCE, FOREIGN_POSTAL_CODE, COUNTRY_NAME, COUNTRY_CODE) >> test_out_file.txt
}' input_inq1.txt