awk multiple-line search and replace one-liner


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk multiple-line search and replace one-liner
# 1  
Old 05-01-2011
[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 02:00 PM..
# 2  
Old 05-01-2011
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.
# 3  
Old 05-01-2011
Code:
awk '/<div id="navbar">/,/<\/div>/ { next } 1' "$file"

# 4  
Old 05-01-2011
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?
# 5  
Old 05-01-2011
Quote:
Originally Posted by louisJ
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"

# 6  
Old 05-01-2011
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... Smilie... 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
# 7  
Old 05-02-2011
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 07:12 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using awk to multiple and replace in a specific line

Hi Folks, I have the file in which I need to multiply the content of a line and replace the initial content of that line with the obtained answer. For example if this is my input file file1.txt 2.259314750 xxxxxx 1.962774350 xxxxxx 2.916817290 xxxxxx 1.355026900 ... (4 Replies)
Discussion started by: Madiouma Ndiaye
4 Replies

2. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

3. Shell Programming and Scripting

awk to search for specific line and replace nth column

I need to be able to search for a string in the first column and if that string exists than replace the nth column with "-9.99". AW12000012012 2.38 1.51 3.01 1.66 0.90 0.91 1.22 0.82 0.57 1.67 2.31 3.63 0.00 AW12000012013 1.52 0.90 1.20 1.34 1.21 0.67 ... (14 Replies)
Discussion started by: ncwxpanther
14 Replies

4. Shell Programming and Scripting

Multiple Line awk search and replace

I have a log file that contains many lines but contains the following line three times: related_pin : "t_bypass"; Here are the 3 occurrences and the two lines after from my file.txt: related_pin : "t_bypass"; sdf_cond : "rstq_b"; timing_sense : negative_unate; ... (6 Replies)
Discussion started by: bobbygb2003
6 Replies

5. Shell Programming and Scripting

replace awk with a perl one liner (REGEXP and FS)

hello, I want to replace awk with a perl one liner in unix. i use in awk REGEX and FS ( field separator) because awk syntaxes in different unix os versions have not the same behaviour. Awk, Nawk and GNU Awk Cheat Sheet - good coders code, great reuse i have a file named "file" and want... (5 Replies)
Discussion started by: bora99
5 Replies

6. UNIX for Dummies Questions & Answers

One liner pattern search with awk/sed/grep

I have an array containing bunch of characters. I have to check this array for specific character and if "Not Found than" use a goto statement to go to USAGE set options = (A B C D E F) @ i = 0 while ($i <= ${#options}) if ($options != "F" || $options != "D") then goto USAGE endif @... (1 Reply)
Discussion started by: dixits
1 Replies

7. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

8. Shell Programming and Scripting

Search & Replace regex Perl one liner to AWK one liner

Thanks for giving your time and effort to answer questions and helping newbies like me understand awk. I have a huge file, millions of lines, so perl takes quite a bit of time, I'd like to convert these perl one liners to awk. Basically I'd like all lines with ISA sandwiched between... (9 Replies)
Discussion started by: verge
9 Replies

9. Shell Programming and Scripting

awk: Multiple search patterns & print in an one liner

I would like to print result of multiple search pattern invoked from an one liner. The code looks like this but won't work gawk -F '{{if ($0 ~ /pattern1/) pat1=$1 && if ($0 ~ /pattern2/) pat2=$2} ; print pat1, pat2}' Can anybody help getting the right code? (10 Replies)
Discussion started by: sdf
10 Replies

10. Shell Programming and Scripting

Does Sed Search/Replace Work For Multiple Matches On The Same Line?

Hello, I would like to delete all the footnotes in all my htm files. Hence, I have to delete the whole font tag pairs, i.e. deleting everything between the begin/end font tags. I create a testfile, of which data parts of all four lines are the same except for the number of font tag pairs,... (3 Replies)
Discussion started by: cibalo
3 Replies
Login or Register to Ask a Question