Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-01-2011
Registered User
 

Join Date: Dec 2010
Posts: 20
Thanks: 4
Thanked 0 Times in 0 Posts
[SOLVED] 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 :


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

Thanks

Last edited by louisJ; 05-08-2011 at 01:00 PM..
Sponsored Links
    #2  
Old 05-01-2011
Peasant's Avatar
Registered User
 

Join Date: Mar 2011
Posts: 365
Thanks: 10
Thanked 68 Times in 67 Posts
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.
Sponsored Links
    #3  
Old 05-01-2011
cfajohnson's Avatar
Shell programmer, author
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,830
Thanks: 0
Thanked 82 Times in 76 Posts

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

    #4  
Old 05-01-2011
Registered User
 

Join Date: Dec 2010
Posts: 20
Thanks: 4
Thanked 0 Times in 0 Posts
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?
Sponsored Links
    #5  
Old 05-01-2011
cfajohnson's Avatar
Shell programmer, author
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,830
Thanks: 0
Thanked 82 Times in 76 Posts
Quote:
Originally Posted by louisJ View Post
thanks cfajohnson
indeed it isolates the right pattern.

But I need to replace the pattern with sub or gsub

You don't need sub or gsub:

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

Quote:
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?

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

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

Sponsored Links
    #6  
Old 05-01-2011
ahamed101's Avatar
root is god!!!
 

Join Date: Sep 2008
Location: India
Posts: 1,478
Thanks: 33
Thanked 382 Times in 377 Posts
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.

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

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

Thank You

regards,
Ahamed
Sponsored Links
    #7  
Old 05-02-2011
Registered User
 

Join Date: Dec 2010
Posts: 20
Thanks: 4
Thanked 0 Times in 0 Posts
Thanks cfajohnson
it is working and it is clean!

Here is the whole line:

Code:
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.

Last edited by louisJ; 05-02-2011 at 06:12 AM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl search and replace - search in first line and replance in 2nd line arvindng Shell Programming and Scripting 3 02-04-2011 09:50 AM
Search Parameter in first line and replace next line content rdtrivedi UNIX for Advanced & Expert Users 4 03-24-2010 11:03 AM
Does Sed Search/Replace Work For Multiple Matches On The Same Line? cibalo Shell Programming and Scripting 3 06-06-2009 03:38 AM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 09:24 AM
Multiple search and replace tungaw2004 UNIX for Dummies Questions & Answers 1 03-21-2007 11:03 PM



All times are GMT -4. The time now is 04:20 AM.