Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004
    Posts
    2

    Unanswered: substitution in sed executed in a script

    I want to substitute the $1 parameter in a script that executes sed, but I cannot get the substitution to work. I can only get SOOOO close. Here is my script so far:

    sed -e 's/foo/$1/g' <infile.nam >outfile.nam

    assuming that the above command is in a script called swapme, then typing:

    >swapme bar

    I want every occurance of "foo" in file infile.nam changed to "bar" in file outfile.nam.

    What I get is every occurance of "foo" in file infile.nam changed to "$1" in file outfile.nam. The $1 substitution does not "take" and so the literal characters $1 are what gets passed to sed. sed does what it's told to do.

    What do I need to do to make the $1 get replaced with "bar"?

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Between simple quotes, the shell doesn't substitute $1.
    Tou must use double quotes.
    Code:
    sed -e "s/foo/$1/g" <infile.nam >outfile.nam
    Jean-Pierre.

  3. #3
    Join Date
    Mar 2004
    Posts
    2
    Excellent! If you only knew how many different things this sed/scripting novice tried! Double quotes instead of single. Woulda thought! Thanks.
    -bt-

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •