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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read from one file-Replace a pattern in another with the current one
# 1  
Old 12-05-2012
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.
# 2  
Old 12-05-2012
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

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 12-05-2012
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

This User Gave Thanks to rdrtx1 For This Post:
# 4  
Old 12-05-2012
Quote:
Originally Posted by bartus11
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
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.
# 5  
Old 12-05-2012
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

This User Gave Thanks to bartus11 For This Post:
# 6  
Old 12-05-2012
Quote:
Originally Posted by bartus11
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.
# 7  
Old 12-06-2012
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to replace a string with pattern read from a file

I have two files blocks.txt and rules.txt. In blocks.txt i have the following entries Linux1 Linux2 Linux3 ..... Linux10 In rules.txt i have the lines where a filename pattern starts like 'blk-name.*' I want to replace 'blk-name' with the names read from blocks.txt file I tried... (2 Replies)
Discussion started by: Jag02
2 Replies

2. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

3. Shell Programming and Scripting

Help with ksh-to read ip file & append lines to another file based on pattern match

Hi, I need help with this- input.txt : L B white X Y white A B brown M Y black Read this input file and if 3rd column is "white", then add specific lines to another file insert.txt. If 3rd column is brown, add different set of lines to insert.txt, and so on. For example, the given... (6 Replies)
Discussion started by: prashob123
6 Replies

4. Shell Programming and Scripting

awk - writing matching pattern to a new file and deleting it from the current file

Hello , I have comma delimited file with over 20 fileds that i need to do some validations on. I have to check if certain fields are null and then write the line containing the null field into a new file and then delete the line from the current file. Can someone tell me how i could go... (2 Replies)
Discussion started by: goddevil
2 Replies

5. Shell Programming and Scripting

Finding 4 current files having specific File Name pattern

Hi All, I am trying to find 4 latest files inside one folder having following File Name pattern and store them into 4 different variables and then use for processing in my shell script. File name is fixed length. 1) Each file starts with = ABCJmdmfbsjop letters + 7 Digit Number... (6 Replies)
Discussion started by: lancesunny
6 Replies

6. UNIX for Dummies Questions & Answers

find the file names having specified pattern at specified position in the current directory

I would need a command for finding first 15000 of the file names whose 25th postion is 5 in the current directory alone. I do have this painful command find . -name '5*' | head -15000 | cut -c3- please refine this. Of course the above command also searches in the sub directories... (3 Replies)
Discussion started by: vk39221
3 Replies

7. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

8. Shell Programming and Scripting

Replace a particular pattern in a file

Hi , I have a requirement where i have this file abcd $$xyz=123 $$zef=789 $$mno=123 $$pqr=456 I need to replace $$pqr from 456 to a new value which will be present in a variable to me. I am not aware of sed and awk functionality. Can somebody please show me how to replace this... (6 Replies)
Discussion started by: lifzgud
6 Replies

9. Shell Programming and Scripting

read from a specific pattern from one file and append it to another

Hi! Everyone, Say this file1 -------------- line 1 51610183 420001010 0010CTCTLEDPPOO 2151610183 line 2 2151610183 420001010 0030A2TH2 line 3 2151610183 420001010 0040A2TH3 line 4 2151610183 420001010 ... (0 Replies)
Discussion started by: kinkar_ghosh
0 Replies

10. Shell Programming and Scripting

read file and print additional rows till current year

Hi all i have a file like 2006,1,2 2007,2,3 2008,3,4 I will read this and my output should be like 2006,1,2 2007,1,2 2008,1,2 2007,2,3 2008,2,3 2008,3,4 Giving the explanation, we will read the first line of the file and if the year any other than current year, we will print as many... (1 Reply)
Discussion started by: vasuarjula
1 Replies
Login or Register to Ask a Question