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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
05-22-2005, 10:28 AM
|
#1
|
Member
Registered: Sep 2003
Location: chikyuu (E103N6)
Distribution: Redhat 8.0 (2.4.25-custom), Fedora Core 1 (2.4.30-custom)
Posts: 357
Rep:
|
sed doesn't accept $variable in bash script
I made this script to replace text using 'sed':
Code:
#!/bin/bash
txt_ori="original text"
txt_rplc="replacement text"
for text in "$@"
do
sed 's/$txt_ori/$txt_rplc/g' $text > text.out
done
But 'sed' doesn't seem to accept the variable. Anyway to make 'sed' to read the variables?
|
|
|
05-22-2005, 10:40 AM
|
#2
|
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994
Rep:
|
Re: sed doesn't accept $variable in bash script
Quote:
Originally posted by chii-chan
I made this script to replace text using 'sed':
Code:
#!/bin/bash
txt_ori="original text"
txt_rplc="replacement text"
for text in "$@"
do
sed 's/$txt_ori/$txt_rplc/g' $text > text.out
done
But 'sed' doesn't seem to accept the variable. Anyway to make 'sed' to read the variables?
|
You are passing two arguments to sed. The first is the literal string s/$txt_ori/$txt_rplc/g, the second is the value of $text
I assume that the value it's not accepting is $txt_ori or $txt_rplc (you can find out exactly what you are passing by using echo instead of sed).
BASH treats anything in single-quotes as a literal string, and will not try to interpolate any variables. You probably want to be using double-quotes instead.
|
|
|
05-22-2005, 11:21 AM
|
#3
|
Member
Registered: Sep 2003
Location: chikyuu (E103N6)
Distribution: Redhat 8.0 (2.4.25-custom), Fedora Core 1 (2.4.30-custom)
Posts: 357
Original Poster
Rep:
|
Thanks for noticing me the single quotes. It seems that 'sed' needs this kind of line:
Code:
sed -e 's/SEARCH_STRING/REPLACE_STRING/g' file > file.out
Using double-quotes i.e.:
Code:
sed -e "s/"$txt_ori"/"$txt_rplc"/g" $text > text.out
doesnt't work. But instead I did this on try and error:
Code:
sed -e s/"$txt_ori"/"$txt_rplc"/g $text > text.out
then it works. 
|
|
|
05-22-2005, 12:26 PM
|
#4
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,858
|
Or use eval bash function like :
Code:
eval "sed -e s/$txt_ori/$txt_rplc/g $text > text.out"
This way you just quote at the beginning and at the end of
the sed line, not inside
|
|
|
05-22-2005, 01:38 PM
|
#5
|
Member
Registered: May 2005
Posts: 378
Rep:
|
Re: sed doesn't accept $variable in bash script
Quote:
Originally posted by chii-chan
I made this script to replace text using 'sed':
Code:
#!/bin/bash
txt_ori="original text"
txt_rplc="replacement text"
for text in "$@"
do
sed 's/$txt_ori/$txt_rplc/g' $text > text.out
done
But 'sed' doesn't seem to accept the variable. Anyway to make 'sed' to read the variables?
|
All you need to do (apart from learning how to treat metacharacters in the shell  ) is use double quotes instead of the single ones, so the $ variables are expanded:
Code:
sed "s/$txt_ori/$txt_rplc/g" $text > text.out
|
|
|
05-25-2005, 07:33 AM
|
#6
|
Member
Registered: Sep 2003
Location: chikyuu (E103N6)
Distribution: Redhat 8.0 (2.4.25-custom), Fedora Core 1 (2.4.30-custom)
Posts: 357
Original Poster
Rep:
|
Thanks for the suggestions. So far the only working one is:
Code:
sed -e s/"$txt_ori"/"$txt_rplc"/g $text > text.out
Code:
sed -e "s/$txt_ori/$txt_rplc/g" $text > text.out
I can't get these to work though:
Code:
eval "sed -e s/$txt_ori/$txt_rplc/g $text > text.out"
But, really, thanks for all of the suggestions since I can apply that to other scripts later.
Last edited by chii-chan; 05-25-2005 at 07:49 AM.
|
|
|
05-28-2005, 07:07 AM
|
#7
|
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994
Rep:
|
Quote:
Originally posted by chii-chan
I can't get these to work though:
Code:
eval "sed -e s/$txt_ori/$txt_rplc/g $text > text.out"
|
This may or may not work, depending on the contents of the values of the variables you are quoting.
If any of them contains a space, this will create a new parameter, which probably isn't what you want. If they contain <chevrons>, then you will start a file redirect. If they contain a pipe (|) then you will start a file redirect.
If you run then you can read all about how and when BASH copes with special characters. For example:
Quote:
eval [arg ...]
The args are read and concatenated together into a single command. This command is then read and exe_cuted by the shell, and its exit status is returned as the value of eval. If there are no args, or only null arguments, eval returns 0.
|
|
|
|
All times are GMT -5. The time now is 06:11 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|