Adding field to file and moving the last 2 fields

I have a file with 32 fields each separated by �|�. I need to add a file date exactly in the format � "20100120" � as the 32nd field moving the existing 32nd field to 33. so the field I added should be 32nd and the 33rd field is the last field before I added the file date.

I know we can use sed and awk to replace or add for a file either at beginning or the end but not in the last but one position. Is there a way to do this?

sed "s/|\([^|]*\)$/|20100120|\1/" file
awk -F"|" -v OFS="|" ' { $(NF+1)=$NF; $(NF-1)="20100120" ; print } ' file

Thanks Anbu. But if have to add the file date from a variable like

file_DATE=$(date +'%Y%m%d')

how should this be re-written

file_DATE=$(date +'%Y%m%d')
sed "s/|\([^|]*\)$/|$file_DATE|\1/" file
file_DATE=$(date +'%Y%m%d')
awk -F"|" -v OFS="|" -v dt=$file_DATE ' { $(NF+1)=$NF; $(NF-1)=dt ; print } ' file
file_DATE=$(date +'%Y%m%d')
awk -F"|" -v OFS="|" ' { $(NF+1)=$NF; $(NF-1)="'"$file_DATE"'" ; print } ' file

thanks Anbu. Its working fine. if i need to have quotations around the file date like "20100125" how can i do that?

sed "s/|\([^|]*\)$/|\"$file_DATE\"|\1/" file
awk -F"|" -v OFS="|" -v dt=$file_DATE -v qt='"' ' { $(NF+1)=$NF; $(NF-1)=qt dt qt ; print } ' file

anbu i dont understand where you have mentioned the field 32, as i have the almost same issue but i need to change field18 and total numbers of fileds are 54.

Thanks

date_now=`date '+%Y%m%d'`
nawk -v datenew="$date_now"  '
{
insert_position=18
for (i=1;i<=NF;i++) {
                   if (i == insert_position) {
                           a=$i
                           $i=datenew
                           printf $i FS a FS
                                           }
                   else {printf $i FS }
                       }
print ""
}
'  FS="|"  InFile.txt

;);):D:D:b::b::cool::cool::cool:

Try...

awk '{$n=$n FS v; print}' FS="|" OFS="|" n=18 v='"201001202"' file