Replace Strings with sed or awk

Hello i need some help with the usage of sed.

Situation : 2 textfiles, file.in , file.out
In the first textfile which is called file.in are the words for the substitution.
Every word is in a new-line like :

Firstsub
Secondsub
Thridsub
...

In the second textflie wich is called file.out is some text and in every line the word SUBSTITUDE exisist.
e.G. :

Lorem ipsum dolor SUBSTITUDE
Ipsum Lorem dolor color SUBSTITUDE
....

I want to replace the words SUBSTITUDE with the wordlist of the first textfile.

The output should look like this:

Lorem ipsum dolor Firstsub
Ipsum Lorem dolor color Secondsub
....

Could anyone help me by the syntax of sed or awk to do this job ?

Thx
Bruce

nawk 'FNR==NR{f1[FNR]=$0;next} /SUBSTITUDE/{sub("SUBSTITUDE", f1[++i])}1' file.in file.out

Here is the solution in sed ,

array=(`grep . file.in`)
i=0;
while read line
do
sub=`sed -r "s/SUBSTITUDE/${array[$i]}/g" <<< $line`
echo $sub >>newfile
let i=$i+1
done < "file.out"

  The file "newfile" will contain the replaced contents .

Thank you for the solution in sed karthigayan, i will try out this method too :slight_smile:

Just a heads up, since we don't know exactly what the data in file.in looks like, if any of it contains characters which are special in sed's substitution command's replacement field, this approach will choke. All it takes is the presence of an "&", "\", or "/".

I'm not saying this approach will not work, but you need to keep this in mind. It depends on what assurances you have regarding the data's content.

Regards,
Alister

Try:

awk '{getline x<f; sub(/SUBSTITUDE/,x)}1' f=file.in file.out