LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-28-2010, 06:18 PM   #1
dougp25
Member
 
Registered: Dec 2003
Location: Seacoast NH
Distribution: PCLinuxOS
Posts: 37

Rep: Reputation: 2
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.
 
Old 10-28-2010, 06:32 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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.?
 
Old 10-29-2010, 01:01 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Assuming you quote the change with single quotes the escaping is not required.
 
Old 10-29-2010, 09:34 AM   #4
dougp25
Member
 
Registered: Dec 2003
Location: Seacoast NH
Distribution: PCLinuxOS
Posts: 37

Original Poster
Rep: Reputation: 2
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.
 
Old 10-29-2010, 09:39 AM   #5
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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.
 
1 members found this post helpful.
Old 10-29-2010, 09:48 AM   #6
dougp25
Member
 
Registered: Dec 2003
Location: Seacoast NH
Distribution: PCLinuxOS
Posts: 37

Original Poster
Rep: Reputation: 2
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.
 
Old 10-29-2010, 09:52 AM   #7
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
replacement with sed disruptive Programming 7 08-11-2010 11:36 AM
Sed pattern replacement icyrail Programming 3 10-27-2009 05:33 AM
Sed column replacement. keysorsoze Programming 11 12-01-2008 09:50 AM
replacement with sed DeepSeaNautilus Programming 6 10-01-2008 06:48 AM
Text replacement question: sed/awk/perl whatever BigRedBall Programming 6 02-05-2008 11:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration