Sed, substitution and spaces

Sed, substitution and spaces

Post by Rick te Linde » Thu, 17 Feb 1994 18:22:27



Given the following small script:

#!/bin/sh
mon=Feb
year=1994
echo title: VAR_TITLE | sed -e 's:VAR_TITLE:'$mon\ $year':'
title=$mon\ $year
echo $title
echo title: VAR_TITLE | sed -e 's:VAR_TITLE:'$title':'

The first sed call provides the desired output, but the second doesn't.
Does anybody know how I can make the second one work?

--
Rick te Lindert                                 *       PO Box 94613
Academic Computing Services Amsterdam (SARA)    *       1090 GP Amsterdam

 
 
 

Sed, substitution and spaces

Post by David W. Tamk » Fri, 18 Feb 1994 14:11:18




| Given the following small script:
|
| #!/bin/sh
| mon=Feb
| year=1994
| echo title: VAR_TITLE | sed -e 's:VAR_TITLE:'$mon\ $year':'
| title=$mon\ $year
| echo $title
| echo title: VAR_TITLE | sed -e 's:VAR_TITLE:'$title':'
|
| The first sed call provides the desired output, but the second doesn't.
| Does anybody know how I can make the second one work?

  echo title: VAR_TITLE | sed -e 's:VAR_TITLE:'"$title"':'

or its simpler equivalent

  echo title: VAR_TITLE | sed -e "s:VAR_TITLE:$title:"

You had an unescaped, unquoted space in the expansion of $title.  Replacing
it with "$title" (soft-quoted rather than unquoted) would cure that.  

David W. Tamkin  P. O. Box 3284  Skokie IL  60076-6284


 
 
 

1. Can sed replace one or more spaces with one space?

I want to turn input that looks like

levin     tty1
root      tty2
rebecca   tty4

to

levin tty1
root tty2
rebecca tty4

Can sed do this?  (My copy doesn't seem to use '+' to mean one or
more.)  (And if it varies, my input is filled with spaces, not
tabs.)

End of question proper.  Follow-up is set to me, I hope.  I'll
summarize.

Other questions:
1.  Would it be wiser to ask this in c.u.shell?
2.  I'm sure there are 40 ways other than sed to do this; if you
    send me other suggestions, I'll summarize.


2. Set up of MAIL

3. command substitution using sed

4. Site in Japan with FVWM 95?

5. sed and variable substitution

6. Best supported fast video card

7. Sed or awk substitution between strings

8. Problem with SLIP

9. sed(?) substitution with $HOME

10. sed substitutions

11. Sed substitution question

12. sed substitution + default valu

13. SED - Help Required with substitution