Sponsored Content
Top Forums Shell Programming and Scripting Script to find & replace a multiple lines string across multiple php files and subdirectories Post 302604322 by spfc_dmt on Sunday 4th of March 2012 11:46:22 AM
Old 03-04-2012
Here it is:

Code:
#!/usr/bin/env ksh

cd /home/username/public_html/tests/
find . -name "*.php" | while read file
do
    echo "munging: $file"             # nice to see progress as it works
    mv "$file" "$file-"      # back it up
    awk '     # read the file and delete the block of php code
    /<?php/ { drop = idx = 0; snarf = 1; }  # start of a block; start buffering

    /?>/ {                  # end of a block
        if( ! drop )        # magic string not found -- show this block
        {
            for( i = 0; i < idx; i++ )
                printf( "%s\n", buffer[i] );
            printf( "%s\n", $0 );
        }

        snarf = 0;          # turn off buffering
        next;
    }

    ### change the string between the slants to be something unique to the block you wish to delete. 
    /PHP_STRING/ { drop = 1; }    # magic string found, drop if we're in a php block

    snarf {                 # if buffering, hold the record until end of block reached.
        buffer[idx++] = $0;
        next;
    }

    { print; }              # not buffering, just print the record.
    '  "$file-" >"$file"
    if (( $? > 0 ))            # handle failure by putting the file back in place
    then
        echo "edit of $file failed" >&2
        mv "$file-" "$file"             # restore original
    else
        rm "$file-"               # worked, delete backup 
    fi
done

Shouldnt the first line be:
Code:
#!/bin/ksh

?? Although i tested this and didnt worked aswel...

Last edited by spfc_dmt; 03-04-2012 at 12:52 PM..
 

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
setbuf(3s)																setbuf(3s)

Name
       setbuf, setbuffer, setlinebuf, setvbuf - assign buffering to a stream

Syntax
       #include <stdio.h>

       void setbuf(stream, buf)
       FILE *stream;
       char *buf;

       void setbuffer(stream, buf, size)
       FILE *stream;
       char *buf;
       int size;

       void setlinebuf(stream)
       FILE *stream;

       int setvbuf(stream, buf, type, size)
       FILE *stream;
       char *buf;
       int type; size_t size;

Description
       The three types of buffering available are unbuffered, block buffered, and line buffered.  When an output stream is unbuffered, information
       appears on the destination file or terminal as soon as written; when it is block buffered many characters are saved up  and  written  as  a
       block;  when  it  is line buffered characters are saved up until a new line is encountered or input is read from stdin.	The routine may be
       used to force the block out early.  Normally all files are block buffered.  For further information, see A buffer is obtained from upon the
       first  or  on  the  file.   If  the  standard stream stdout refers to a terminal it is line buffered.  The standard stream stderr is always
       unbuffered.

       The routine is used after a stream has been opened but before it is read or written.  The character array buf is used instead of  an  auto-
       matically  allocated  buffer.  If buf is the constant pointer NULL, input/output will be completely unbuffered.	A manifest constant BUFSIZ
       tells how big an array is needed:
       char buf[BUFSIZ];

       The routine, an alternate form of is used after a stream has been opened but before it is read or written.  The character array	buf  whose
       size  is  determined  by  the  size  argument  is  used instead of an automatically allocated buffer.  If buf is the constant pointer NULL,
       input/output will be completely unbuffered.

       The routine is used to change stdout or stderr from block buffered or unbuffered to line buffered.  Unlike and it can be used at  any  time
       that the file descriptor is active.

       The  routine  may  be  used  after a stream has been opened but before it is read or written.  Type determines how stream will be buffered.
       Legal values for type, defined in stdio.h are:

	_IOFBF	      causes input/output to be fully buffered.

	_IOLBF	      causes output to be line buffered; the buffer will be flushed when a new line is written, the buffer is full,  or  input	is
		      requested.

	_IONBF	      causes input/output to be completely unbuffered.

       If  buf is not the NULL pointer, the array it points to will be used for buffering, instead of an automatically allocated buffer.  The size
       specifies the size of the buffer to be used.  The constant BUFSIZ in <stdio.h> is suggested as a good  buffer  size.   If  input/output	is
       unbuffered, buf and size are ignored.

       By default, output to a terminal is line buffered and all other input/output is fully buffered.

       A  file	can be changed from unbuffered or line buffered to block buffered by using For further information, see A file can be changed from
       block buffered or line buffered to unbuffered by using followed by with a buffer argument of NULL.

Restrictions
       The standard error stream should be line buffered by default.

       The and functions are not portable to non 4.2 BSD versions of UNIX.

See Also
       malloc(3), fclose(3s), fopen(3s), fread(3s), getc(3s), printf(3s), putc(3s), puts(3s).

																	setbuf(3s)
All times are GMT -4. The time now is 11:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy