Read from one file-Replace a pattern in another with the current one | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER 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 Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-05-2012
jacobs.smith jacobs.smith is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 27 August 2014, 11:38 AM EDT
Posts: 299
Thanks: 254
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 12-05-2012
bartus11's Avatar
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 31 August 2014, 5:32 PM EDT
Posts: 3,710
Thanks: 7
Thanked 1,142 Times in 1,113 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
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 15 July 2014, 11:14 AM EDT
Location: Houston, Texas, USA
Posts: 675
Thanks: 0
Thanked 203 Times in 195 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
jacobs.smith jacobs.smith is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 27 August 2014, 11:38 AM EDT
Posts: 299
Thanks: 254
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 12-05-2012
bartus11's Avatar
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 31 August 2014, 5:32 PM EDT
Posts: 3,710
Thanks: 7
Thanked 1,142 Times in 1,113 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
jacobs.smith jacobs.smith is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 27 August 2014, 11:38 AM EDT
Posts: 299
Thanks: 254
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 12-06-2012
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 1 September 2014, 3:25 AM EDT
Location: Amsterdam
Posts: 9,371
Thanks: 272
Thanked 2,341 Times in 2,101 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 Thread

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 03:31 AM.