LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-13-2007, 05:46 PM   #1
chilebiker
Member
 
Registered: Jun 2006
Location: Lake District, Chile
Distribution: Arch
Posts: 50

Rep: Reputation: 17
Sed doesn't update file in bash script [solved]


I'm writing a bash script that updates my website, but I'm having a problem with sed.

Code:
sed -ri 's:blablabla:muahaahaa:' test.html
works on the command line, but using the same command in the script doesn't update my file. My test script:
Code:
#!/bin/bash

Test_File="test.html"
if [ "$1" -eq 1 ]
then
  Search_String="blablabla"
  Replace_String="muahaahaa"
else
  Search_String="muahaahaa"
  Replace_String="blablabla"
fi

sed -ri 's:$Search_String:$Replace_String:' $Test_File
echo "old: ${Search_String}"
echo "new: ${Replace_String}"

exit 0
I've been working on this all afternoon, but haven't found a solution. Any help appreciated!

Edit: I've been posting this a couple of minutes too early, I found the solution. The sed-command in the script must be without single quotes (').

Last edited by chilebiker; 06-13-2007 at 05:59 PM. Reason: found solution
 
Old 06-13-2007, 06:09 PM   #2
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
I think you need single quotes around the variable so that sed does not think it is a regular expression.
Code:
sed -ri 's:'$Search_String':'$Replace_String':' $Test_File
 
Old 06-13-2007, 06:46 PM   #3
chilebiker
Member
 
Registered: Jun 2006
Location: Lake District, Chile
Distribution: Arch
Posts: 50

Original Poster
Rep: Reputation: 17
@dxqcanada
Yes, that works as well. Might be safer to use it your way - thanks!
 
Old 06-13-2007, 09:43 PM   #4
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by chilebiker
I'm writing a bash script that updates my website, but I'm having a problem with sed.

Code:
sed -ri 's:blablabla:muahaahaa:' test.html
works on the command line, but using the same command in the script doesn't update my file. My test script:
Code:
#!/bin/bash

Test_File="test.html"
if [ "$1" -eq 1 ]
then
  Search_String="blablabla"
  Replace_String="muahaahaa"
else
  Search_String="muahaahaa"
  Replace_String="blablabla"
fi

sed -ri 's:$Search_String:$Replace_String:' $Test_File
echo "old: ${Search_String}"
echo "new: ${Replace_String}"

exit 0
I've been working on this all afternoon, but haven't found a solution. Any help appreciated!

Edit: I've been posting this a couple of minutes too early, I found the solution. The sed-command in the script must be without single quotes (').

But do enclose it (and the filename) in double quotes:

Code:
sed -ri "s:$Search_String:$Replace_String:" "$Test_File"
 
Old 06-14-2007, 08:25 AM   #5
chilebiker
Member
 
Registered: Jun 2006
Location: Lake District, Chile
Distribution: Arch
Posts: 50

Original Poster
Rep: Reputation: 17
After plenty of testing and playing around i found out that the following code works best:
Quote:
sed -ri s:"$Search_String":"$Replace_String": $File_Name
No quotes for s:..:..:, but both arguments enclosed in double quotes

Last edited by chilebiker; 06-14-2007 at 08:26 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
running sed inside script file bajaj111 Linux - Software 9 11-08-2006 09:23 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
sed doesn't accept $variable in bash script chii-chan Programming 6 05-28-2005 07:07 AM
problem with sed in a bash script nexus55 Linux - Software 6 05-03-2004 09:40 PM
sed in small BASH script OhLordy Linux - General 1 08-29-2003 11:32 AM


All times are GMT -5. The time now is 04:31 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration