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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Substitute a string with file contents

Status
Not open for further replies.

pwnawannab

Technical User
Mar 8, 2007
8
US
Having trouble constructing proper PERL command to perform substitution of a string with in one file with contents of another file. I am working in ksh. Here is what I am trying:

substitute_variable='some_file'
perl -p -e 's/search_string/$substitute_variable/' output_file

The result is that search_string is blank after execution. I am not sure if something is up with perl command or I am just mixing up ksh with perl.

Please let me know if you have any thoughts on how to perform this.

Thanks,
 
Thanks MillerH read that one - that's the structure I used but it only inserts a blank where the string is. I would like to place several lines of text instead of certain string though.

I have no problem substituting static string in one file for another static string. My case is a little different though I use variable to read in my report

Sort of like if original text is:
ListOfVeggies:
MyList
Now I want to substitute string MyList with a list contained in some file. So I read in the list into a shell variable and try the perl command from the above. That is not working out for me.
 
Thanks KevinADC. Very helpful page - bookmarked. but I did not find did not find anything I could use here. I am not very familiar with perl but I'll give it another look to see maybe I missed something.
 
Maybe the perl command isn't being constructed how you think in the ksh script? Try putting an
Code:
echo perl -p -e 's/search_string/$substitute_variable/' output_file
in your script and see what prints out on the screen...

Also, as you obviously have perl on your system, stop messing about with ksh and do the whole thing in perl...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks Steve,
I am actually reworking a ksh script, after this particular sed routine started encountering buffer overflow. Someone told me PERL is the way to go, but unfortunately this is the first time I am using it. So I was hoping I can do quick and dirty swap from sed to perl right with in the script.

ok echoing the command; looks like it builds it properly. the return on the command execution is
Substitution replacement not terminated at -e line 1.

My input file is multi-line. All other variables that use perl statement and are a single word substitutions work. So it is the multi-line file that I am trying to write instead of the string. Any suggestions on how should I terminate the lines?

Thanks,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top