Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-05-2012
Registered User
 
Join Date: Jan 2012
Posts: 267
Thanks: 222
Thanked 4 Times in 4 Posts
Read from one file-Replace a pattern in another with the current one

Hi Friends,

I have a text file like this


Code:
cat main.txt

I like this website

cat website > new_website

grep website > hello_website


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


Code:
cat main1.txt

I like this hello

cat hello > new_hello

grep hello > hello_hello


Code:
cat main2.txt

I like this unix

cat unix > new_unix

grep unix > hello_unix


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


Code:
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.
Sponsored Links
    #2  
Old 12-05-2012
bartus11's Avatar
Registered User
 
Join Date: Apr 2009
Posts: 3,139
Thanks: 3
Thanked 954 Times in 933 Posts
Try:
Code:
awk 'NR==FNR{a[++n]=$0;next}{for (i=1;i<=n;i++){x=a[i];gsub ("website",$0,x);print x > "main"FNR".txt"}}' main.txt replace.txt

The Following User Says Thank You to bartus11 For This Useful Post:
jacobs.smith (12-05-2012)
Sponsored Links
    #3  
Old 12-05-2012
Registered User
 
Join Date: Sep 2012
Location: Houston, Texas, USA
Posts: 571
Thanks: 0
Thanked 173 Times in 167 Posts
also try:

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

The Following User Says Thank You to rdrtx1 For This Useful Post:
jacobs.smith (12-05-2012)
    #4  
Old 12-05-2012
Registered User
 
Join Date: Jan 2012
Posts: 267
Thanks: 222
Thanked 4 Times in 4 Posts
Quote:
Originally Posted by bartus11 View Post
Try:
Code:
awk 'NR==FNR{a[++n]=$0;next}{for (i=1;i<=n;i++){x=a[i];gsub ("website",$0,x);print x > "main"FNR".txt"}}' main.txt replace.txt

Hi Bartus,

Thanks for your time.

This is the error I am seeing


Code:
awk: syntax error at source line 1
 context is
	NR==FNR{a[++n]=$0;next}{for (i=1;i<=n;i++){x=a[i];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 ----------

Quote:
Originally Posted by rdrtx1 View Post
also try:

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

Hi rdrtx1,

I neither see an error, nor my output files.

Thanks for your time though.
Sponsored Links
    #5  
Old 12-05-2012
bartus11's Avatar
Registered User
 
Join Date: Apr 2009
Posts: 3,139
Thanks: 3
Thanked 954 Times in 933 Posts
What operating system are you using? If Solaris then use nawk:
Code:
nawk 'NR==FNR{a[++n]=$0;next}{for (i=1;i<=n;i++){x=a[i];gsub ("website",$0,x);print x > "main"FNR".txt"}}' main.txt replace.txt

The Following User Says Thank You to bartus11 For This Useful Post:
jacobs.smith (12-06-2012)
Sponsored Links
    #6  
Old 12-05-2012
Registered User
 
Join Date: Jan 2012
Posts: 267
Thanks: 222
Thanked 4 Times in 4 Posts
Quote:
Originally Posted by bartus11 View Post
What operating system are you using? If Solaris then use nawk:
Code:
nawk 'NR==FNR{a[++n]=$0;next}{for (i=1;i<=n;i++){x=a[i];gsub ("website",$0,x);print x > "main"FNR".txt"}}' main.txt replace.txt

I am using the terminal on my Mac OSX.

It says nawk command not found.
Sponsored Links
    #7  
Old 12-06-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,337
Thanks: 143
Thanked 1,754 Times in 1,591 Posts
Slight modification to Bartus11's awk:

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

Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk - writing matching pattern to a new file and deleting it from the current file goddevil Shell Programming and Scripting 2 12-01-2012 03:57 PM
Finding 4 current files having specific File Name pattern lancesunny Shell Programming and Scripting 6 11-02-2012 02:32 PM
find the file names having specified pattern at specified position in the current directory vk39221 UNIX for Dummies Questions & Answers 3 08-30-2011 07:59 PM
Replace a particular pattern in a file lifzgud Shell Programming and Scripting 6 11-24-2010 11:12 AM
read file and print additional rows till current year vasuarjula Shell Programming and Scripting 1 12-09-2008 02:13 AM



All times are GMT -4. The time now is 11:36 AM.