|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
[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.phpbut 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
|
||||
|
||||
|
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
|
||||
|
||||
|
|
#4
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
Quote:
|
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
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
|
|||
|
|||
|
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 | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|