Re: sed: variables in regular expression

From: Icarus Sparry (usenet_at_icarus.freeuk.com)
Date: 06/16/05


Date: Thu, 16 Jun 2005 19:30:14 GMT

On 2005-06-16, nasir <nasir.kamal@gmail.com> wrote:
> In my script instead of doing this:
> sed 's/nasir/NasirA/g' list.txt
>
> I want to use this:
> sed 's/$smallname/$uppername/g' list.txt
> where $smallname is "nasir" and $uppername is "NasirA".
>
> But sed is not taking variable names, and its output is the same as the
> original file although the file list.txt contains the string "nasir". I
> know it's easy for sed gurus.

This is a shell quoting problem. use double quotes if you want shell
variables to be recognised, rather than single quotes.
        sed "s/$smallname/$uppername/g" list.txt



Relevant Pages

  • Re: Please help me understand this expression
    ... The original script attempts to convert from one database type to another. ... The file menu.db is copied below the bash script lines. ... Rule #1, you can't put a single quote inside single quotes, not even if you escape it. ... Basically, the whole quoted perl command, the thing between the single quotes '.....' ...
    (comp.lang.perl.misc)
  • Re: [PHP] var_dump() results
    ... login or password to the list, so I tried setting $host, $login, ... and $table variables at the top of the script. ... The reason your variables didn't work was that you had them enclosed in single quotes. ... # I used curly brackets around my variable, just so the PHP parse will ...
    (php.general)
  • Internal -e escape char
    ... I did this because it is quick and easy and I can use it when all I have is web access.And I don't have to create a new script everytim I want to see which is fsatest. ... now with that same idea in mind I'd like top have a form I couls enter som perl and have it execute it via perl -e. ... I could replace all single quotes with double quotes and escape everythgin inbetween them but that seems like a lot. ... (Since shell escape characters may or may not work since apache is executing it) ...
    (perl.beginners)
  • bash variable expansion oddity
    ... I've written a Bash shell script that delivers unexpected results when ... My aim is to have the line that begins 'sudo' expand to the following: ... (Notice the single quotes around the tokens "host and hostname.com".) ...
    (comp.unix.shell)
  • Re: Shell Script Variable Usage
    ... > The script does not work. ... The problem is the $PCPU varible. ... The reason it doesn't work is that the single quotes tell the shell NOT to ... interpret anything inside - so what is passed is the string $PCPU, ...
    (comp.unix.sco.misc)