stallion has asked for the wisdom of the Perl Monks concerning the following question:

Is there any regex to delete a complete line and replace with another line....

For eg: macros perl macros monks macros one liner macros have to be converted as macros regex macros regex macros regex macros regex

Whatever follows macros whether it is empty or followed by a string have to be replaced with regex and All macros will be at the beginning of the line so we can include ^ character in the regex.

Thanks Monks

Replies are listed 'Best First'.
Re: Perl One Liner Regular Expressions
by cheekuperl (Monk) on Aug 16, 2012 at 11:20 UTC
    Ammm...btw..one way of doing it is like this:
    sed -n -e 's/^macros .*/macros regex/gp' <file>
Re: Perl One Liner Regular Expressions
by uday_sagar (Scribe) on Aug 16, 2012 at 11:44 UTC

    Stallion,

    Put your contents in monks.txt and give the following one liner.

    perl -p -i -e "s/^macros(.)*/macros\ regex/g" monks.txt

    :-)

      No need for parentheses; nor to escape the space; nor for the 'g' modifier - '.*' will gobble everything up, thus there can be only one match. So:

      perl -p -i -e "s/^macros.*/macros regex/" monks.txt # or perl -p -i -e "s/^macros\K.*/ regex/" monks.txt
      The grouping is not really required. This works pretty fine:
      s/^macros.*/macros regex/
Re: Perl One Liner Regular Expressions
by cheekuperl (Monk) on Aug 16, 2012 at 11:17 UTC
    Show us your code :)