Another sed question - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Another sed question

XfactorXfactor Posts: 343Member
I'm trying to insert text before a "matched regular expression". The only thing is, I need to match a variable after the regex. For example:

sed '/regex $var/i
str $var' filename;

This is one of many things I've tried. Does anyone know the correct way?

Comments

  • fedfed Posts: 18Member
    : I'm trying to insert text before a "matched regular expression". The only thing is, I need to match a variable after the regex. For example:
    :
    : sed '/regex $var/i
    : str $var' filename;
    :
    : This is one of many things I've tried. Does anyone know the correct way?
    :

    First, shells do not expand variables when single-quoted.
    So, your code might look like:

    [code]
    $ sed "s/regex $var/i str $var/" filename
    [/code]

    or so.

    Second, for such complicated searches, you can use Perl one-liners.
Sign In or Register to comment.