LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Use sed, replacement text has quotes (https://www.linuxquestions.org/questions/programming-9/use-sed-replacement-text-has-quotes-841050/)

dougp25 10-28-2010 06:18 PM

Use sed, replacement text has quotes
 
I am trying to do a pretty large replacement of text, and using some bash scripting and sed, I am making progress (thanks to many of you here)!

Anyway, my script is still not doing what I want it to do, so I decided to do one substitution manually from the bash prompt:

sed s/_BROWSER_TITLE/"School Management System"/ admin_start_1.php

When I look in the php file, I see this:

<title><?php echo School Management System?></title>

I wanted the text being substituted to retain its quotes.

GrapefruiTgirl 10-28-2010 06:32 PM

Code:

echo "<title><?php echo _BROWSER_TITLE?></title>" |\
  sed 's|_BROWSER_TITLE|\"School Management System\"|'

# outputs:

<title><?php echo "School Management System"?></title>

Perhaps you just need to escape the quotation marks, using backslashes like this.?

grail 10-29-2010 01:01 AM

Assuming you quote the change with single quotes the escaping is not required.

dougp25 10-29-2010 09:34 AM

Well I must be missing some sort of switch or something. I use this:

sed 's|_BROWSER_TITLE|\"School Management System\"|' filename.php

The file echos back to me on the screen and shows the substitution successfully. However, a cat of the file or if I vi the file, the change has not occurred.

Reading the man page, but if you have the quicker answer, I'd like it!

Thanks.

GrapefruiTgirl 10-29-2010 09:39 AM

Yes, I gave the example to demonstrate the quotes and back-slashes, thinking you would adjust the surrounding code as required, or alter your existing code..

If you want it to actually edit a file inplace, use the -i switch to sed:
Code:

sed -i 's|_BROWSER_TITLE|\"School Management System\"|' filename.php
That -i makes it edit the file. Sorry about that, I should have mentioned it too. ;)

dougp25 10-29-2010 09:48 AM

Thanks GrapefruiTgirl! I am so new to sed, and have tried awk as well as perl scripts! I need to just stick with sed until I am done this project. I think this is gonna do me. If not, I'll open a new thread.

thanks again.

GrapefruiTgirl 10-29-2010 09:52 AM

No problem.

But, don't necessarily try to "stick to sed" for a particular project, or you may end up trying to mold it around some problem where another tool such as awk would be better. If you get stuck on it, or on another tool syntax/usage problem, just make a new thread.

There's loads to all these tools - I only know a relatively small part of each of them - but lots of other members have more experience with them and will help out as necessary.

Cheers!
PS - if you are happy with this thread's answer, you can mark it SOLVED using thread tools menu above the first post. EDIT: You got it.


All times are GMT -5. The time now is 07:27 AM.