Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.