[relug] Re: Sostituore testi

  • From: Francesco Zamboni <f.zamboni@xxxxxxxxx>
  • To: relug@xxxxxxxxxxxxx
  • Date: Fri, 22 Apr 2005 10:56:09 +0200

On 4/22/05, Luca Bedogni <bedogniluca-linux@xxxxxxxx> wrote:
> Tralasciando l'orrendo italiano del Topic -_-
> 
> Ho la necessita di cambiare piccole parti all'interno di una grossa
> quantita di file testuali. Del tipo, ho gia presenti molti file che
> contengono abc, devo cambiare queste stringhe in abcd ad esempio. Spero di
> essermi spiegato. Conoscete un qualche programma che possa fare al caso
> mio? Se non c'è nulla lo faro ma se c'e gia meglio :) Testuale o grafico
> mi va utto bene, devo semplicemente cambiare parti di testo su tutti i
> file presenti in una directory..
> 
> Ciao
>         Bedo
> --
> Debian Powered GNU/Linux User #373118
> Bedogni Luca - http://bedo.homelinux.org
> Debianizzati - www.debianizzati.org | Founder Member
> --
> Eletti sono coloro per i quali le cose belle non hanno altro significato
> che di pura bellezza.  Oscar Wilde
> 


Beh, per un lavoretto simile qualche tempo fa mi ero fatto un brutale
script php... io ti riporto il sorgente di seguito, ma dacci un'
occhiata prima di usarlo perche e' passato un bel po da quando l' ho
scritto (e usato) e quindi non mi ricordo neanche se tutti i bug erano
stati rimossi, o se l' avevo lasciato in condizioni utilizzabili...

Ah, inoltre, siccome avevo usato il metodo "brutale" di caricare in
memoria tutto il file di testo da maneggiare siccome lo dovevo usare
solo per file piccolini, se i tuoi file sono molto grossi potresti
avere dei problemi...

#! /usr/bin/php -q
<?php

$help_requested         =array_search("-h",$argv);
$to_be_substituted_k    =array_search("-s",$argv);
$to_substitute_k        =array_search("-d",$argv);
$to_eliminate_cr        =array_search("-cr",$argv);

if ($help_requested > 0)
{
        print "usage: replacer [-s <string-to-be-substituted>] [-d
<string_to_substitute>] [-cr] files ... \n -cr \t deletes carriage
returns from files\n\n Search for a string in a list of test files and
substitute it with another given string. Optionally can also delete CR
characters, leaving only LF";
}
else
{
        $matpar = array();
        if ($to_be_substituted_k > 0)
        {
                $matpar[] = $to_be_substituted_k;
                $matpar[] = $to_be_substituted_k+1;
        }
        if ($to_substitute_k > 0)
        {
                $matpar[] = $to_substitute_k;
                $matpar[] = $to_substitute_k+1;
        }
        if ($to_eliminate_cr > 0)
        {
                $matpar[] = $to_eliminate_cr;
        }
        for($cont=1;$cont<$argc; $cont++)
        {
                if (!(array_search($cont,$matpar) > -1))
                {
                        $file_pattern[]         =$argv[$cont];
                        //echo $cont . "\n";
                }
                //echo $cont . " - ". array_search($cont,$matpar) . "\n";
        }
        $to_be_substituted      =$argv[$to_be_substituted_k + 1];
        $to_substitute          =$argv[$to_substitute_k + 1];

        foreach($file_pattern as $file_path)
        {
                echo $file_path . "\n";
                $file_pointer = fopen($file_path, "r");
                $content=fread($file_pointer,filesize($file_path));
                fclose($file_pointer);
                if (strlen($to_be_substituted)> 0)
                {
                        
$content=str_replace($to_be_substituted,$to_substitute,$content);
                }
                if ($to_eliminate_cr > 1)
                {
                        $content=str_replace(chr(13),"",$content);
                }
                $file_pointer = fopen($file_path, "w");
                fwrite($file_pointer,$content);
                fclose($file_pointer);
        }
}
?>

Other related posts: