awk multiple-line search and replace one-liner

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 :

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 the deletion of the record separator?

Thanks

Can you please post exact input and desired output.
With that folks here probably provide best solution.

Just lookin @ it, i believe sed could be possibly better tool for the job.

Regards
Peasant.

awk '/<div id="navbar">/,/<\/div>/ { next } 1' "$file"

thanks cfajohnson
indeed it isolates the right pattern.

But I need to replace the pattern with sub or gsub
and also to write to a file with the same name as the input file (but to avoid file cut due to buffer).
Do you know how to?

You don't need sub or gsub:

sub=XXXXXXX
awk -v sb="$sub" '/<div id="navbar">/,/<\/div>/ { if ( $0 ~ /<\/div>/ ) print sb; next } 1' "$file"

Write to a temporary file and copy that over the original:

tempfile=tempfile$$
YOURCOMMAND "$file" > "$tempfile" && mv "$tempfile" "$file"

you mean you need to replace the pattern <div>...</div> with something else?

regards,
Ahamed

---------- Post updated at 03:21 PM ---------- Previous update was at 03:20 PM ----------

well, johnson already replied... :)... cheers!

---------- Post updated at 03:25 PM ---------- Previous update was at 03:21 PM ----------

Johnson, I have a doubt.

awk '/<div id="navbar">/,/<\/div>/ { next } 1' "$file"

/srchptrn1/,/srchptrn2/ => means from srchptrn1 to srchptrn2 ? or is it something else

Thank You

regards,
Ahamed

Thanks cfajohnson
it is working and it is clean!

Here is the whole line:

file="activites.php"; tempfile="temp.php"; sub="               <?php include(\"menu_event.php\") ?>;"; mawk -v sb="$sub" '/<div id="navbar">/,/<\/div>/ { if ( $0 ~ /<\/div>/ ) print sb; next } 1' "$file" > "$tempfile" && mv "$tempfile" "$file"

Do you think it would possible to pipe to this the results from a find command (find . -maxdepth 1 -type f -not \( -name "menu.php" \) -not \( -name "boutique_.php" \) -not \( -name "*connect*" \) -not \( -name "0sp*." \) -not \( -name "0en*." \) -name "event_*.php") and still work with the variables to avoid buffer issues ?

Thank you very much.

ok I think I've got it right:

jojo=`find . -maxdepth 1 -type f -not \( -name "*menu.php" \) -not \( -name  "boutique_*.php" \) -not \( -name "*connect*" \) -not \( -name "0sp*.*"  \) -not \( -name "0en*.*" \)  -name "event_*.php"`; for file in $jojo; do 
file="activites.php"; tempfile="temp.php"; sub="               <?php include(\"menu_event.php\") ?>;"; mawk -v sb="$sub" '/<div id="navbar">/,/<\/div>/ { if ( $0 ~ /<\/div>/ ) print sb; next } 1' "$file" > "$tempfile" && mv "$tempfile" "$file"; done

Or pipe find into a while loop:

find. <whatyouneedtofind> | while read file ; do
  <whatyouneedtodowith> "$file"
done
1 Like