Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Substitute text in a file for text in another

Status
Not open for further replies.

stackdump

Technical User
Sep 21, 2004
278
GB
I want to find the presence of some text (which is contained in a file) within the text of another file and then replace it with the text in another file.

So I have fileA which contains some text to search for, fileB with the text I want to change it to, and fileC which is the target. So I effectively want to do something along the lines of;

cat fileC | sed -e 's/content of fileA/content of fileB/g'

Nothing I've tried so far has worked. Any ideas?
 
Hi

If b.txt and c.txt contains no special characters :
Code:
[blue]master #[/blue] cat a.txt
hello africa
tell me how you doing

[blue]master #[/blue] cat b.txt
a
e
i
o
u

[blue]master #[/blue] cat c.txt
A
E
I
O
U

[blue]master #[/blue] paste -d / b.txt c.txt | sed 's/.*/s\/&\/g/' | sed -f - a.txt
hEllO AfrIcA
tEll mE hOw yOU dOIng

Feherke.
 

That's good but not quite what I'm trying to do. Here's an example of what I'm attempting....

Target.txt;
Here is my poem;
Mary had a little lamb,
it's fleece was white as snow.
Everywhere that Mary went
the lamb was sure to go.
The End.

Search.txt;
Mary had a little lamb,
it's fleece was white as snow.
Everywhere that Mary went

Substitution.txt;
Barry had a big fat lamb,
it was was really, really slow.
Everywhere that Barry went

Target.txt (result)
Here is my poem;
Barry had a big fat lamb,
it was was really, really slow.
Everywhere that Barry went
the lamb was sure to go.
The End.

 
Hi

With [tt]sed[/tt] is abit complicated because of the embeded new-lines. But with [tt]awk[/tt] is easyer :
Code:
awk 'BEGIN{RS=""}{s[ARGIND]=$0}END{print gensub(s[2],s[3],"",s[1])}' fileA fileB fileC

Feherke.
 
Hi

Yes, you are right. When have metacharacters too, is better to use a more powerfull language.
Code:
perl -n0e 'if($.<3){${"s$."}=$_}else{~s/\Q$s1\E/$s2/;print}' fileB fileC fileA

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top