|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
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:
I neither see an error, nor my output files. Thanks for your time though. |
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
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
|
|||
|
|||
|
Quote:
It says nawk command not found. |
| Sponsored Links | |
|
|
#7
|
||||
|
||||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|