replace words in file based on another file

Hello,
Can someone kindy help me solve this problem..I am using SunOS shell script
I got a file A with following content:

This is my correct document. I wrote 111
This is my incorrect word , 222
This is my wrong statement 333
This is my correct document 444
This is my correct document 555

if the fifth word is document, then replace the word with a new word according to the config file.

If the content of config file B is
111,one
555,five

then the result updated in file A shoud be:

This is my correct document. I wrote one
This is my incorrect word , 222
This is my wrong statement 333
This is my correct document 444
This is my correct document five

even document is the fifth element, 444 will not be updated because 444 is not appeared in config file...

Can someone show me how to accomplish this? thx!

In what language/utility? I can do in PERL. Takes only a few lines, but to get it right (and elegant) I might have to tinker for a while....

what have you tried so far ? Please show us that. We could help you in completing that

Try:

awk '
NR==FNR {
   split($0, cfg, /,/);
   config[cfg[1]] = cfg[2];
   next;
}
$5~/^document/ && ($NF in config) {
  $NF = config[$NF];
}
1
' B A

Jean-Pierre.

Hi aigles,
would u please kindly explain the program.. For usage of awk, I ony know how to use it to parse a string. So, I dun quite get the meaning of each ine of code.. The script should be purely unix script, not per script

awk '

NR==FNR {                           # Select records from first file
   split($0, cfg, /,/);             #    Split record with "," into record cfg
   config[cfg[1]] = cfg[2];         #    Memorize config value
   next;                            #    Proceed next record
}                                   #

# The following code is for second file
                                     
$5~/^document/ && ($NF in config) { # Select records starting with "document" and
                                    #  last fields memorized in config record (build
									#  from first file)
  $NF = config[$NF];                #    Set last field with corresponding value from config array
}                                   #
 
1                                   # Print record (maybe modified above)

' B A

Jean-Pierre.

thx alot for yr explanation

Hi,
regarding the reply, the script has been modified to cope with new requirement of checking last servel characters in a column for replacement.

config file:
001,333

port.csv:
1,2,3,4,5,6,7,8,A-B-001,NNN

expected result:
1,2,3,4,5,6,7,8,A-B-333,NNN

new script:
#!/usr/bin/ksh
#input file for process
FILENAME=port.csv
#config file for replacement
CONFIG_TXT=cfg.txt
#output file for result
OUTPUT=port_result.csv

#awk command
nawk 'BEGIN {FS=OFS=","}
NR==FNR{arr[$1]=$2;next}
$0 !~ "Documentation"{print $0;next}
{split($9,a,"-")}
a[3] in arr{$11=arr[a[3]]}1' $CONFIG_TXT $FILENAME > $OUTPUT

==================
thw above script works wel in nawk. However, I later found out that there is no nawk command in a server and awk must be used. No matter how I modified the script using awk.. it won't work..can someone help me? FNR not supported in awk?

Hi all,
how to re-write the code in perl?

nawk 'BEGIN{FS=","}
{
if(NR==FNR)
	a[$1]=$2
else
{
	if(index($0,"document")!=0)
	{	for (i in a)
		{	
			if(index($0,i)!=0)
				gsub(i,a,$0)
			
		}
		print
	}
	else
	{
		print
	}
}
}' b a