Read from one file-Replace a pattern in another with the current one

Hi Friends,

I have a text file like this

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.

Thanks in advance.

Try:

awk 'NR==FNR{a[++n]=$0;next}{for (i=1;i<=n;i++){x=a;gsub ("website",$0,x);print x > "main"FNR".txt"}}' main.txt replace.txt
1 Like

also try:

c=1 ; while read w ; do sed 's/website/'"$w"'/g' main.txt > main$c.txt ; (( c = c + 1 )) ; done < replace.txt
1 Like

Hi Bartus,

Thanks for your time.

This is the error I am seeing

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 ----------

Hi rdrtx1,

I neither see an error, nor my output files.

Thanks for your time though.

What operating system are you using? If Solaris then use nawk:

nawk 'NR==FNR{a[++n]=$0;next}{for (i=1;i<=n;i++){x=a;gsub ("website",$0,x);print x > "main"FNR".txt"}}' main.txt replace.txt
1 Like

I am using the terminal on my Mac OSX.

It says nawk command not found.

Slight modification to Bartus11's awk:

awk '
  NR==FNR{
    a[++n]=$0
    next
  }
  {
    for (i=1;i<=n;i++){
      x=a
      gsub ("website",$0,x)
      f="main" FNR ".txt"
      print x > f
    }
  }
' main.txt replace.txt

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

You mean sth like this..:slight_smile:

$ 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.