LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-08-2010, 10:03 AM   #1
mslucas
LQ Newbie
 
Registered: Mar 2010
Posts: 2

Rep: Reputation: 0
Question: Sed and variables problem


Hi,

I'm writing a small bash script. The problem is that sed isn't working as I hope it would.

TEST=h[o]i
echo $TEST > testfile
sed -i "s/$TEST/hoi/" testfile
cat testfile

I would want the result to be hoi and not h[o]i

Does somebody have any nice commend how to solve this problem?

With kind regards,

Maurice Lucas
 
Old 03-08-2010, 10:19 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
I can tell you WHY it's happening, but don't have a perfect solution at this time.

Also, any solution or help provided to you, might be particular to a certain version of sed; since you don't tell us what OS this is happening on, and you have posted this question from a Windows machine, it may be relevant if you tell us what OS this problem is occurring on, and which version of sed, as well as the ultimate purpose of this script -- i.e is there an end result that you may be able to accomplish by a different method? Or is this just some homework..

As you may be aware, the [ and ] characters are special characters when using regular expressions, and those characters in your $TEST variable are being expanded and then taken to be parts or a regex, rather than literally.

Two workarounds:

1) check the variable for special characters, and escape them prior to using the variable in the LHS of the sed command.

or

2) Forget about using $TEMP variable, and do the replacement with a literal string, with the special chars escaped.

Sasha
 
Old 03-08-2010, 10:34 AM   #3
mslucas
LQ Newbie
 
Registered: Mar 2010
Posts: 2

Original Poster
Rep: Reputation: 0
sed --version
GNU sed version 4.1.5

cat /etc/issue
Ubuntu 8.04.4 LTS \n \l

This is a very simplified version of my script. I get a list of Windows directory's on a remote server and I have to check if the backup of every directory did run and is complete.

So i made a "website" which the setup of

dirname resultfield datefield

The sed line will search for the dirname in the html file and change the resultfield in failure/success and a nice color.

I can say they aren't allowed to insert escape characters on that server but it is beter to fix my script that to try t change my client.


The line below is from my real script and does work perfect, as long as tehre aren't any escape characters.
sed -i 's@<tr><td>$RBACKUPDIR.*@<tr><td>$RBACKUPDIR</td><td bgcolor="#FF0000">Failed</td><td>`date +"%A %d %B %Y %H:%M"`</td>@' /var/www/index.html
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed and variables oden Programming 9 07-29-2007 04:36 PM
variables within sed substitutions? ocicat Programming 3 07-29-2007 12:17 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
using bash variables in sed jimieee Programming 6 02-04-2004 04:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:32 AM.

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