Sponsored Content
Top Forums Shell Programming and Scripting Script to find & replace a multiple lines string across multiple php files and subdirectories Post 302605172 by agama on Tuesday 6th of March 2012 08:20:15 PM
Old 03-06-2012
Sorry for the confusion, yes I only pasted the awk portion figuring you could insert that into your script body.

Very strange. I took this to a different machine (FreeBSD) just to see if a different flavour of awk might barf and its only compplaint was to escape the question mark in the following line (new character in red):

Code:
/\?>/ {                  # end of a block

The Gnu awk on my Linux host wasn't complaining about that. What version of awk do you have installed?
Code:
awk --version

should give that to you. I've been testing this with GNU Awk 3.1.6.

To test a bit further....
I've cut/pasted the test file I'm using and it doesn't have any issues. I took the awk straight from your post just to be sure and used the dummy "UNIQUE STRING" as well. The result, when I execute it, is the middle section is dropped.

Code:
 <?php
   = "SELECT * FROM articles WHERE id = '".['article']."'";
  ->doQuery();
   = ->getRows();
  if(isset(['add'])) {
    if(trim(['nick']) != '') {
       = trim(['nick']);
    } else {
      ['nick'] = 'Please enter your nickname.';
    }
    if(trim(['comment']) != '') {
       = trim(['comment']);
    } else {
      ['comment'] = 'Please enter a comment.';
    }
    if(empty()) {
       = "INSERT INTO comments (website, article_id, nickname, message, email) VALUES ('".['website']."','".['article']."','".."','".."','".."')";
      ->doQuery();
      header('Location: '.['HTTP_REFERER']);
    }
  }
  ?>
<?php get_header(); ?><?php
   = "SELECT * FROM articles WHERE id = '".['article']."'";
  ->doQuery();
   = ->getRows();
  if(isset(['add'])) {
    if(trim(['nick']) != '') {
       = trim(['nick']);
    } else {
      ['nick'] = 'Please enter your nickname.';
    }
    if(trim(['comment']) != '') {
       = trim(['comment']);
    } else {  //"UNIQUE CODE"
      ['comment'] = 'Please enter a comment.';
    }
    if(empty()) {
       = "INSERT INTO comments (website, article_id, nickname, message, email) VALUES ('".['website']."','".['article']."','".."','".."','".."')";
      ->doQuery();
      header('Location: '.['HTTP_REFERER']);
    }
  }
  ?>
  <?php
   = "SELECT * FROM articles WHERE id = '".['article']."'";
  ->doQuery();
   = ->getRows();
  if(isset(['add'])) {
    if(trim(['nick']) != '') {
       = trim(['nick']);
    } else {
      ['nick'] = 'Please enter their nickname.';
    }
    if(trim(['comment']) != '') {
       = trim(['comment']);
    } else {
      ['comment'] = 'Please enter a comment.';
    }
    if(empty()) {
       = "INSERT INTO comments (website, article_id, nickname, message, email) VALUES ('".['website']."','".['article']."','".."','".."','".."')";
      ->doQuery();
      header('Location: '.['HTTP_REFERER']);
    }
  }
  ?>



What happens if you save just the awk in a file (lets say test_awk), the data in test_data and try this:

Code:
ksh test_awk <test_data

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and Replace in multiple files (Shell script)

hi guys, Suppose you have 100 files in a folder and you want to replace all occurances of a word say "ABCD" in those files with "DCBA", how would you do it ??? jatin (13 Replies)
Discussion started by: jatins_s
13 Replies

2. UNIX for Dummies Questions & Answers

Find and replace a string in multiple files

I used the following script cd pathname for y in `ls *`; do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y; done and it worked fine for finding and replacing strings with names etc. in all files of the given path. I'm trying to replace a string which consists of path (location of file) ... (2 Replies)
Discussion started by: pharos467
2 Replies

3. Shell Programming and Scripting

replace multiple lines in multiple files

i have to search a string and replace with multiple lines. example Input echo 'sample text' echo 'college days' output echo 'sample text' echo 'information on students' echo 'emp number' echo 'holidays' i have to search a word college and replace the multiple lines i have... (1 Reply)
Discussion started by: unihp1
1 Replies

4. Shell Programming and Scripting

shell script to find and replace string in multiple files

I used the following script cd pathname for y in `ls *`; do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y; done and it worked fine for finding and replacing strings with names etc. in all files of the given path. I'm trying to replace a string which consists of path (location of file) ... (11 Replies)
Discussion started by: pharos467
11 Replies

5. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

6. Shell Programming and Scripting

Single/Multiple Line with Special characters - Find & Replace in Unix Script

Hi, I am creating a script to do a find and replace single/multiple lines in a file with any number of lines. I have written a logic in a script that reads a reference file say "findrep" and populates two variables $FIND and $REPLACE print $FIND gives Hi How r $u Rahul() Note:... (0 Replies)
Discussion started by: r_sarnayak
0 Replies

7. Shell Programming and Scripting

how can i find number of lines in files & subdirectories

how can i find number of lines in files & subdirectories ? (3 Replies)
Discussion started by: pcbuilder
3 Replies

8. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

9. Shell Programming and Scripting

Search & Replace: Multiple Strings / Multiple Files

I have a list of files all over a file system e.g. /home/1/foo/bar.x /www/sites/moose/foo.txtI'm looking for strings in these files and want to replace each occurrence with a replacement string, e.g. if I find: '#@!^\&@ in any of the files I want to replace it with: 655#@11, etc. There... (2 Replies)
Discussion started by: spacegoose
2 Replies

10. Shell Programming and Scripting

Replace a string with multiple lines

Hello Guys, I need to replace a string with multiple lines. For eg:- ABC,DEF,GHI,JKL,MNO,PQR,STU need to convert the above as below:- ABC,DEF, GHI1 GHI2 GHI3, JKL,MNO, PQR1 PQR2 PQR3, STU i have tried using code as:- (2 Replies)
Discussion started by: jassi10781
2 Replies
All times are GMT -4. The time now is 09:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy