Hi,
I am writing a code where the file is a pipe delimited and I would need to extract the 2nd part of field2 if it is "ATTN", "C/O" or "%" and check to see if field9 is populated or not. If field9 is already populated then leave it as is but if field9 is not populated then take the 2nd part of data from field2 (if ATTN, C/O or %) and move it to field9.
I have the code written but as I am reading 1 line at a time it is taking a very very long time to write the output. I would really appreciate if I can get some help on it. The code is below.
For Example :
Example Data
Input File :
Field2 Field9
Record 1 : 123 Main St, ATTN : James Bond |John Smith
Record 2 : 199 6th St, ATTN: Mary Jones |
Output File :
Field2 Field9
Record 1 : 123 Main St, |John Smith
Record 2 : 199 6th St, | Mary Jones
cat test_file.dat | while read line
do
temp_field9=`echo $line | awk 'BEGIN{FS="|"}{print $9}END { }'`
temp_attn=`echo $line | awk 'BEGIN{FS="ATTN"}{print $2}END { }' | awk 'BEGIN{FS="|"}{print $1}END { }'`
temp_co=`echo $line | awk 'BEGIN{FS="C/O"}{print $2}END { }' | awk 'BEGIN{FS="|"}{print $1}END { }'`
temp_pct=`echo $line | awk 'BEGIN{FS="%"}{print $2}END { }' | awk 'BEGIN{FS="|"}{print $1}END { }'`
if ( [ ! $temp_field9 ] ) then
if ( [ $temp_attn ] ) then
temp_field9=`echo $temp_attn`
elif ( [ $temp_co ] ) then
temp_field9=`echo $temp_co`
elif ( [ $temp_pct ] ) then
temp_field9=`echo $temp_pct`
fi
fi
if ( [ $temp_attn ] ) then
field2=`echo $temp_field2 | awk 'BEGIN{FS="ATTN"}{print $1}END { }'`
echo $field2> ${STP_HOME_DIR}/temp_company.dat
elif ( [ $temp_co ] ) then
field2=`echo $temp_field2 | awk 'BEGIN{FS="C/O"}{print $1}END { }'`
echo $field2> ${STP_HOME_DIR}/temp_company.dat
elif ( [ $temp_pct ] ) then
field2=`echo $temp_field2 | awk 'BEGIN{FS="%"}{print $1}END { }'`
echo $field2> ${STP_HOME_DIR}/temp_company.dat
else
echo $line | awk 'BEGIN{FS="|"}{print $2}END { }'> ${STP_HOME_DIR}/temp_company.dat
fi
echo $field2"|"$temp_field9>> new_test_file.dat
done
Please advise.
Thanks,...