Find the answer to your Linux question:
Results 1 to 4 of 4
Hi I am trying to search and replace a multi line pattern in a php file using awk. The pattern starts with <div id="navbar"> and ends with </div> and spans ...
  1. #1
    Just Joined!
    Join Date
    Nov 2010
    Posts
    15

    awk multiple line search and replace

    Hi

    I am trying to search and replace a multi line pattern in a php file using awk.
    The pattern starts with
    <div id="navbar">
    and ends with
    </div>
    and spans over an unknown number of lines.

    I need the command to be a one liner.
    I use the "record separator" like this :

    Code:
    awk -v RS="</div>" -v FILENEW="nouveau.php" '{gsub(/<div\ id\=\"navbar\">.*$/,"<?php include(\"menu.php\"); ?>",$0); print $0 "</div>">FILENEW; system("mv " FILENEW " " FILENAME)}' myfile.php
    but it deletes all the
    </div>
    in the file.
    How can I avoid this?

    Thanks
    Last edited by louisJ; 05-01-2011 at 06:05 PM.

  2. #2
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52
    Code:
    awk '/<div id="navbar">/,/<\/div>/ { next } 1' "$file"

  3. #3
    Linux Enthusiast Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    717
    Or use an xsl transformation, which I regard as the preferred way to proces xml documents. OTOH awk is good with data files where the data is structured vertically (in lines).

  4. #4
    Just Joined!
    Join Date
    Nov 2010
    Posts
    15
    Thanks.
    This thread is being answered on unix.com
    with the thread name: awk multiple-line search and replace one-liner

Posting Permissions

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