#!/bin/sh
mv eg.txt eg.input
awk 'NR==FNR{a[++i]=$2;next}{sub("#P#",a[++j]);print>(FILENAME".new")}' eg.input ./*.txt &&
for f in *.txt; do mv "$f.new" "$f"; done
mv eg.input eg.txt
eg.txt is renamed to eg.input and then back so that *.txt in the awk line expands only to the files that should be modified.
NR==FNR{ #For the first file, eg.input
a[++i]=$2 #Put the second field in the array `a`
next #Skip the rest of the code
}
{ #For the other files
sub("#P#",a[++j]) #Make the substitution
print>(FILENAME".new") #Print to the line to `FILENAME`.new
}
Then, in a for loop, the old *.txt files contents are overwritten by the *.new files contents. You may want to suppress the for loop until you are convinced that the *.new files are correct.
Some awk implementations do not handle many open files (GNU awk does). If your awk exits with "too many open files" error, use this variant,
awk 'NR==FNR{a[++i]=$2;next}FNR==1{close(fn);fn=FILENAME".new"}{fn=FILENAME;subsub("#P#",a[++j]);print>(fn".new");print>fn}'