cat main.txt
I like this website
cat website > new_website
grep website > hello_website
cat replace.txt
hello
unix
apple
Now, for each line read in 2.txt, I want the pattern "website" in 1.txt to be replaced with it. Basically, I will be getting wc number of files in 2.txt
So, my final output files will be
cat main1.txt
I like this hello
cat hello > new_hello
grep hello > hello_hello
cat main2.txt
I like this unix
cat unix > new_unix
grep unix > hello_unix
cat main3.txt
I like this apple
cat apple > new_apple
grep apple > hello_apple
I got an ide of the following, but I think I am missing something
for i in `cat replace.txt`; do sed '/website/$i/s';done
But, I am having trouble on listing the files and updating the file numbers.
awk: syntax error at source line 1
context is
NR==FNR{a[++n]=$0;next}{for (i=1;i<=n;i++){x=a;gsub ("website",$0,x);print x > >>> "main"FNR <<< ".txt"}}
awk: illegal statement at source line 1
---------- Post updated at 03:46 PM ---------- Previous update was at 03:45 PM ----------
Hello,
The awk code is really fascinating and very useful.However it implements a global change
Could it be changed slightly to accomodate the following data structure in the "master file" i.e. the file from which the changes are to be made.
The file could have the structure
i.e. wherever you find string a replace by string b
An example would make this clear:
This provides explicit replace "rules"and only John would be replaced in the master file and not Johnny.
If the code is modified to meet this requirement, it would be of great use to people like me who work with dictionaries
Many thanks
$ cat file1
John=Jean
Mary=Marie
$ cat file2
John rocks the stage
js Mary sfds sdf
mof dfs dfd Johny sdfd
$ awk 'NR==FNR{A[NR]=$0;next}{for(j in A){split(A[j],P,"=");for(i=1;i<=NF;i++){if($i==P[1]){$i=P[2]}}}}1' file1 file2
Jean rocks the stage
js Marie sfds sdf
mof dfs dfd Johny sdfd
Many thanks. Yes and it works fabulously on a short sample which I tried. I'll test it on a very large sample and get back to you in case there are any glitches.