Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

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

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-05-2012
jacobs.smith jacobs.smith is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 30 June 2015, 2:34 PM EDT
Posts: 324
Thanks: 278
Thanked 5 Times in 5 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 Unix and Linux 12-05-2012
bartus11's Unix or Linux Image
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 28 June 2015, 6:17 PM EDT
Posts: 3,733
Thanks: 7
Thanked 1,152 Times in 1,122 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 Unix and Linux 12-05-2012
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 25 June 2015, 6:26 PM EDT
Location: Houston, Texas, USA
Posts: 682
Thanks: 0
Thanked 206 Times in 198 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 Unix and Linux 12-05-2012
jacobs.smith jacobs.smith is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 30 June 2015, 2:34 PM EDT
Posts: 324
Thanks: 278
Thanked 5 Times in 5 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 Unix and Linux 12-05-2012
bartus11's Unix or Linux Image
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 28 June 2015, 6:17 PM EDT
Posts: 3,733
Thanks: 7
Thanked 1,152 Times in 1,122 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 Unix and Linux 12-05-2012
jacobs.smith jacobs.smith is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 30 June 2015, 2:34 PM EDT
Posts: 324
Thanks: 278
Thanked 5 Times in 5 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 Unix and Linux 12-06-2012
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 4 July 2015, 5:47 AM EDT
Location: Amsterdam
Posts: 10,301
Thanks: 346
Thanked 2,721 Times in 2,434 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
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 06:08 AM.