Cut 2 fields and write to a output file

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,...

The desc is not so clear enough on $9 rules. Please explain more detail.

Please provide sample with all date fields.

Here is draft code for you.

$  cat input.txt
Record 1 : 123 Main St, ATTN : James Bond |John Smith ::::::
Record 2 : 199 6th St, C/O: Mary Jones | James :::::::::

bwa89@tcs-c90f903ef65 ~/temp
$ aawk -F ":" '{split($2,a,",")} {if (a[2]~/ATTN|C\/O|%/) {$2=a[1]",";split($3,b,"|");$3=""; $9="|"b[2]}}1' OFS=":" input.txt
Record 1 : 123 Main St,:::::::|John Smith
Record 2 : 199 6th St,:::::::| James :::

If field9 already have data populated then we need to leave the data "As Is". Else we take 2nd part of data from field2, if field2 have ATTN, C/O or %, and move it to field9 on output.

Thanks....

 
For Example :
 
Input Record : 
 
Record 1 : 123 Main St ATTN  James Bond |John Smith 
Record 2 : 199 6th St C/O Mary Jones |
 
Output should be : 
Record 1 : 123 Main St |John Smith 
Record 2 : 199 6th St| Mary Jones

see if you want to extract your change your given input to your out put use this sed command.

Example:

sed -e  " s/ATTN \| C\/O/\|/g; s/\([A-Z][a-z]* [A-Z][a-z]*\) |\([A-Z][a-z]* [A-Z][a-z]*\)/\1/g;s/[|]$//g "; fine-name

Out put:

Record 1 : 123 Main St | James Bond
Record 2 : 199 6th St| Mary Jones