Not able to pass string with spaces in shell

I have to pass a sentence in a file, the specs are as:

cat run | sed 's/SRT/'$8'/g'  | sed 's/plength/68/g'  | sed 's/stcol/'$5'/g' | sed 's/encol/'$6'/g' | sed 's/brdtype/'$1'/g' | sed 's/brdtxt/'$3'/g' | sed 's/demotxt/Total '$2'/g' | sed 's/bantxt/ban_'$7'/g' | sed 's/validcodes/'$4'/g' > runx
TEXT = "Power Tools or Accessories"
source run.do POWERTOOLS "$TEXT" powertools 012:042 1242 4242 power1 nosort

Here at demotxt I have to pass a string "Power Tools or Accessories", but i am getting only blanks with this specs in the output file

Out put I am getting

*include idemo.axe;btxt=Total 

and output desired is

*include idemo.axe;btxt=Total Power Tools or Accessories

Please help me out...

Not very clear
what contains 'run' ?
what's 'run.do' ?
the sed line could be written whithout cat & pipe :

 sed -e 's/SRT/'$8'/g' -e 's/plength/68/g' -e 's/stcol/'$5'/g' -e 's/encol/'$6'/g' -e 's/brdtype/'$1'/g' -e 's/brdtxt/'$3'/g' -e 's/demotxt/Total '$2'/g' -e 's/bantxt/ban_'$7'/g' -e 's/validcodes/'$4'/g' run > runx

My suspicion is that nothing is wrong with your sed line (save for the fact that it could be written more easily as frans has pointed out) but the problem is here (marked red):

TEXT = "Power Tools or Accessories"

In practically all shells whitespace are special characters and there mustn't be any spaces in variable declarations:

x="abc"
x = "abc"

will yield different results with the first line probably giving the expected one.

I hope this helps.

bakunin

I have tried by deleting blanks but it's thorwing error like unidentified reference...

and then I have changed it as TEXT ="Power Tools or Accessories"
then I am getting blank in the output

i am working on some another softwere and "run" is the file I actually need to run, but i have about 50 statements like the one I am passing and just to avoind the lenght of the scripting i want to pass this somwhow....

---------- Post updated at 02:06 AM ---------- Previous update was at 02:01 AM ----------

Thanks Frans, i am really not that familiar with shell and trying to make my work somewhat easier by googling for shell, and really thnx for letting e know this new way of scrpting..

not TEXT ="Power Tools or Accessories"
but TEXT="Power Tools or Accessories"

have tried this one also but it's throwing error and output came as blank completely

In this case post some complete data: the script, the input, the associated files, the desired output. Otherwise it is like trying to solve a puzzle you are not allowed to even know.

bakunin

It will create new file "runx"

I want to see the result at line number 211 in run file:
main file to edit "run": *include idemo.axe;btxt=demotxt

i want to change demotxt to "Power Tools or Accessories" and again I have few more things to pass,

after this I am using file run.do which will update run file as runx with the help of file source where i am passing all the parameters

Please let me know if you need anything else...

Everyone at the UNIX and Linux Forums gives their best effort to reply to all questions in a timely manner. For this reason, posting questions with subjects like "Urgent!" or "Emergency" and demanding a fast reply are not permitted in the regular forums.

For members who want a higher visibility to their questions, we suggest you post in the Emergency UNIX and Linux Support Forum. This forum is given a higher priority than our regular forums.

Posting a new question in the Emergency UNIX and Linux Support Forum requires forum Bits. We monitor this forum to help people with emergencies, but we do not not guarantee response time or best answers. However, we will treat your post with a higher priority and give our best efforts to help you.

If you have posted a question in the regular forum with a subject "Urgent" "Emergency" or similar idea, we will, more-than-likely, close your thread and post this reply, redirecting you to the proper forum.

Of course, you can always post a descriptive subject text, remove words like "Urgent" etc. (from your subject and post) and post in the regular forums at any time.

Thank you.

The UNIX and Linux Forums

The problem throughout this thread is the single quote characters. You cannot nest single quote characters. They are treated in pairs. In fact the middle two single quotes are not required in any of the sed commands posted.

sed 's/brdtxt/'$3'/g'
Thus the substitition parameter $3 is left outside of the scope of either pair of quotes. It sort of works until there are space characters in $3 - then the sed command errors.

Consider these simplified examples:

echo "brdtxt"|sed 's/brdtxt/'Banana'/g'
Banana

echo "brdtxt"|sed 's/brdtxt/'Power Tools or Accessories'/g'
sed: Function s/brdtxt/Power cannot be parsed.


Now we remove the middle two single quote characters and it starts working.

echo "brdtxt"|sed 's/brdtxt/Power Tools or Accessories/g'
Power Tools or Accessories

echo "brdtxt"|sed 's/brdtxt/"Power Tools or Accessories"/g'
"Power Tools or Accessories"

Thnaks for letting me know the system present at your end, I will keep this in mind from next time.

---------- Post updated at 08:04 PM ---------- Previous update was at 07:59 PM ----------

Thanks Methyl! But I want to pass about 50 brand text in this file and hence was trying to pass them from outside, if it is not posible to pass a sentence with SHELL then I will have to use the method you have suggested but it really will not shorten the script which i want to.

Thanks & Regards,
Rakesh Patil
<email address removed>

That was only an example to justify removing the inner pair of single quotes.
However once we are inside a shell script we need to lose the single quotes completely or they will stop the substitution of $3.

sed 's/brdtxt/'$3'/g'
Change to:
sed -e "s/brdtxt/$3/g"

Obviously the same error is in all the "sed" statements but it only goes wrong of the parameter contains space characters.

Thanks Methyl, it's now working, thanks very much! :b:

Something else : The variables within the sed expression shouldn't be quoted but the whole expressions must be between double quotes like:

sed -e "s/SRT/$8/g" -e "s/plength/68/g" -e "s/stcol/$5/g" -e "s/encol/$6/g" -e "s/brdtype/$1/g" -e "s/brdtxt/$3/g" -e "s/demotxt/Total $2/g" -e "s/bantxt/ban_$7/g" -e "s/validcodes/$4/g" run > runx

Thanks Frans, Will change my script accordingly from next time!

Thanks all of you for the support, solution and letting me know the better way of programming shell!

Rakesh
patilrakesh1984@gmail.com