Batch edit files
Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: Batch edit files

  1. #1
    Flash M0nkey
    Join Date
    Sep 2001
    Posts
    3,447

    Batch edit files

    Ok quick ques (2 today from me oO)

    once again am going back to site have/am designing.

    I want to change an element of each page - the code for each os the same

    basically it will be a straight find/replace on each page

    the code to be replaced is the same on each page as is the new code am writing in its place.

    Now i could go through each page 1 at a time and do a ctrl+h but that would take a while. Is there anyway to automate it?

    Like a program i could load all the files that need changed into specify the text to find what to replace it with and let it do the hard work?

    v_Ln

  2. #2
    Flash M0nkey
    Join Date
    Sep 2001
    Posts
    3,447
    oh one thing i forgot to add - have tried some search/replace programs which handle multiple files but my problem is that the text

    Code:
    	<div id="container">
    		<div id="menu">
    			<a href="index.php">Home</a>
    			<a href="lab.php">The Lab </a>
    			<a href="archive.php">Archives</a>
    			<a href="digiart.php">Digiart</a>
    			<a href="b3ta.php">b3ta</a>
    			<a href="photos.php">Photos</a>
    		<a href="contact.php">Contact</a></div>
    stretches over multiple lines and most only aloow me to paste in the code up until the first 'return'

    v_Ln

  3. #3
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi valhallen

    In the past, before php, dreamweaver and such, I did create
    a small c program, which more or less does what you want to do
    now. Find it attached (compiles as well under linux as windows).
    But - before you use it: create a backup (the program itself
    creates a backup before changing the files, but one never knows...)!

    I am not aware of some tools or scripts existing which are doing
    this. To write a script takes quite some effort, I think (hence, a
    c (or perl) program).

    Nowadays, if php is enabled, I would do the following, for future
    references:

    1) store that whole navigation part in a file "navigation.php"
    2) change the text in the files to
    Code:
    <?php
    	include ('navigation.php');
    ?>
    3) In case of any upcoming change, you have to change one file only.


    Cheers
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  4. #4
    Senior Member
    Join Date
    Dec 2004
    Posts
    107
    What I would suggest is that if you have code that repeats (such as a menu that appears on many pages), you could always separate it out and do an include statement in your html files (forgot the syntax, it's been years!). Not only does this make it more modular and easier for you to change things in the future, it is nowadays considered proper html coding.

    You can check out many sites to see how this can be done:

    www.w3schools.com
    www.devguru.com (my personal favourite)

    ... and pretty much any html tutorial site that explains some CSS will help you.

    hope this helps.
    -ik

  5. #5
    Leftie Linux Lover the_JinX's Avatar
    Join Date
    Nov 2001
    Location
    Beverwijk Netherlands
    Posts
    2,534
    I think you mean the php include(); function..

    Btw, V_Ln if you need it bad, see me in IRC tomorrow, I'll whip you up a bash+sed script to do the job..
    ASCII stupid question, get a stupid ANSI.
    When in Russia, pet a PETSCII.

    Get your ass over to SLAYRadio the best station for C64 Remixes !

  6. #6
    Flash M0nkey
    Join Date
    Sep 2001
    Posts
    3,447
    thanks for the replies guys

    as for the php includes - thats what am trying to replace it with - lol

    i noramlly do code all static elements which are seen on every page (like navigation) as a sperate file which is pulled in at runtime....but i didn't this time as I didn't foresee the site being all that large - guess who was wrong! So i thought I'd better do it now before it got any bigger - lol

    will take a look at your prog hopefully tomorrow sec - thanks

    v_Ln

  7. #7
    Leftie Linux Lover the_JinX's Avatar
    Join Date
    Nov 2001
    Location
    Beverwijk Netherlands
    Posts
    2,534
    By popular request..

    Code:
    #!/bin/bash
    
    # Bash multi line remove and replace with new line(s)..
    # Will seek first instance of end starting with start.
    # Creates filename.bak backup file.
    #
    # (c) 2005 Anne Jan Brouwer (the_JinX[at]etv.cx)
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License for more details.
    #
    # http://www.gnu.org/copyleft/gpl.html
    
    # Define Start element for replace
    start='<div id="container">'
    
    # Define End element for replace
    end='</div>'
    
    # Replacement string (can be multiline)
    replace="<?php
       include( newstuff.php ); 
    ?>"
    
    if [[ $* == ""  ]]
    then
        echo "Usage: $0 file(s)"
        exit 1
    fi
    
    # setup integers
    declare -i startline
    declare -i endline
    declare -i tail
    declare -i eof
    
    for phpfile in $*
    do
       if [[ `grep "$start" "$phpfile"`  ]]
       then
          mv "$phpfile" "$phpfile.bak"
          eof=`wc -l "$phpfile.bak" | sed -e "s/$phpfile.bak//"`
          startline=`/usr/bin/grep -nm1 "$start"  "$phpfile.bak" | cut -d ":" -f 1`
          let "startline = $startline - 1"
          head -n $startline "$phpfile.bak" > "$phpfile"
          echo "$replace" >> "$phpfile"
          let "tail = $eof - $startline"
          tail -n $tail "$phpfile.bak" > "$phpfile.tmp"
          endline=`/usr/bin/grep -nm1 "$end" "$phpfile.tmp" | cut -d ":" -f 1`
          eof=`wc -l "$phpfile.tmp" | sed -e "s/$phpfile.tmp//"`
          let "tail = $eof - $endline"
          tail -n $tail "$phpfile.tmp" >> "$phpfile"
          rm "$phpfile.tmp"
       else
          echo "'start' not found, skipping $phpfile..."
       fi
    done
    Asuming you called it modify and did a chmod +x modify
    You could use it like this..

    ./modify index.php otherfile.php /home/myhome/www/yetanotherfile.php

    It does have a tiny bit of error handling (skip and warn if 'start' isn't found)..
    ASCII stupid question, get a stupid ANSI.
    When in Russia, pet a PETSCII.

    Get your ass over to SLAYRadio the best station for C64 Remixes !

  8. #8
    Leftie Linux Lover the_JinX's Avatar
    Join Date
    Nov 2001
    Location
    Beverwijk Netherlands
    Posts
    2,534
    I got a pm about this script giving errors when the start delimiter is found more then once in a file..
    The error is in line
    Code:
    startline=`/bin/grep -n "$start"  "$phpfile.bak" | cut -d ":" -f 1`
    A fix for this would be grep -nm1 instead of -n limiting the output to one (the first) instance..
    In which case only the first instance of the start delimiter will ne used.

    (edited previous post, but wanted to let you know about my mistake )
    Bounds checking is vital.. even in scripts (especialy in scripts ??)
    ASCII stupid question, get a stupid ANSI.
    When in Russia, pet a PETSCII.

    Get your ass over to SLAYRadio the best station for C64 Remixes !

  9. #9
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    I'm disappointed the_JinX... You promised us a bash+sed script and then came up with bash only solution.
    Code:
    $ sed -i.old '/.*<div id="container">$/,/.*<\/div>$/c<?php\
       include( newstuff.php );\
    ?>' file1 file2 file3 ...
    I'm not sure which platform you're using val, but I tested it on cygwin using GNU sed. It even make a backup before doing the replacement.

    Peace always,
    <jdenny>
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


  10. #10
    Leftie Linux Lover the_JinX's Avatar
    Join Date
    Nov 2001
    Location
    Beverwijk Netherlands
    Posts
    2,534
    Nice one, jdenny..

    But mine is bigger then yours

    For some reason that kind of logic just doesn't work on scripts

    I was under the assumption that sed wouldn't work on multi-line stuff, since it evaluates one line at a time..
    Will have to read up on sed though... (insight welcome)..
    ASCII stupid question, get a stupid ANSI.
    When in Russia, pet a PETSCII.

    Get your ass over to SLAYRadio the best station for C64 Remixes !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •