1

I am using read to make some file in a while loop. The code is:

while read var val
do
 sed -i 's/$var/$val/g' Hhh300_4L_gen.sh
 echo $var $val
done < "Hhh300_4L_config.txt"

Where in Hhh300_4L_config.txt, there is a line, for instance,

PROCESSNAME Hhh;

and in Hhh300_4L_gen.sh, there is one element: PROCESSNAME. So if it works, PROCESSNAME in Hhh300_4L_gen.sh should be replaced by Hhh. But it doesn't. However the output of echo prints correctly.

6
  • 1
    's/$var/$val/g' -> "s/$var/$val/g", otherwise your variable references won't be expanded. Commented Jun 29, 2017 at 14:59
  • If you're having the double quoted sed choke on a line, try adding set -x to the script before the loop so you can see what the commands look like after the variables are expanded. Then you can find the value that is causing the problem--probably one that has / in it is my guess Commented Jun 29, 2017 at 16:24
  • Ah, thanks. The empty line cause the problem. After I delete all the empty lines, it works. So in this case, we can't put any empty lines in .txt file? Commented Jun 29, 2017 at 16:41
  • I added few commands to skip those empty lines. Thanks for your help! Commented Jun 29, 2017 at 17:06
  • If there is a character that doesn't show up in any of them, like, say, # then you could use that as the sed delimeter: sed -i "s#$var#$val#g' you don't have to use / if there's something that works better for you Commented Jun 29, 2017 at 17:11

1 Answer 1

3

Variables are not expanded inside single quotes. If you want var and val to expand you need double quotes (and make sure they don't have the sed separator, here / in them):

sed -i "s/$var/$val/g" Hhh300_4L_gen.sh

though if you're modifying a shell script (as I'm guessing might be from the .sh) there are probably better ways to do it, like having your .txt file store things as var=val instead of with white space separating them, then just source it from the script.

Sign up to request clarification or add additional context in comments.

5 Comments

Also thanks for all your replies. In fact, that is what I first used. If using double quotes, I would get error:sed: -e expression #1, char 0: no previous regular expression. Then I changed to sed -i "s/'$var'/'$val'/g" Hhh300_4L_gen.sh, didn't work, either. And just to correct the example, it should be PROCESSNAME Hhh300_4L.
@maosenzhou that sounds like you probably have a / in one of your values
I attach a part of .txt file here: ##process name PROCESSNAME Hhh300_4L ##process config MODEL Hheft_UFO2 DEFINE define w = w+ w- define l = l+ l- define vv = vl vl~ PROCESSCMD generate p p > h2, (h2 > h h, (h > w+ w-, w > l vv), (h > w+ w-, w > l vv)) (Since I'm new to this, I don't how to make a list here..each block is corresponding to a line the .txt file) Does it look OK? By the way, .sh file is not a real shell script, which will be passed to another package.
@maosenzhou comments don't format very well, and other people won't find the information very readily in them. Instead, could you please edit your question with the new information. In general it's helpful if you have sample input, desired output and actual error messages and the like to show how things aren't working as desired
I edit it, each block is corresponding to a line. Basically, I want each variable in Hhh300_4L_gen.sh is replaced by its value. But it doesn't. Hhh300_4L_gen.sh keeps the same. But read and while work nicely, so I guess the problem hides in the line of sed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.