条件付き置換の書き方

条件付き置換の書き方

作業ディレクトリ内の単語を置き換えたい->$\to$単語が逐語的に環境。

擬似コード

すべてのエントリを置き換えます->$\to$他の場所では、しかし内部では\begin{逐語的}...\end{逐語的}

これを基にした私のPerl擬似コード素晴らしい答え

#!/usr/bin/env perl

## The target directories
my @directories=("Cardiology", "Pathophysiology", "Patology and Biopsy", "Physiology", "Propedeutics", "Radiology", "Rheumatology", "Surgery");


## Iterate over the directories
foreach my $dir (@directories) {
    my $dd=0;
    ## Read the current directory
    opendir (my $DIR, "$path/$dir");
    ## Find all files in this directory
    while (my $file = readdir($DIR)) {
        ## Skip any files that aren't .tex
        next unless $file =~ /\.tex$/;

        ## Open the file
        open(my $fh,"$path/$dir/$file");

        while (<$fh>) {
            if (/\\begin{verbatim}/) {                    
                # Skip the Verbatim environment # TODO how this?
                # Need to tell it that you one line by one until \end{verbatim} is met, after which go normally forward.
                while (!/\\end{verbatim}/) {
                    $dd++;
                }
                $dd++;
            }
            else {
                if ($dd==0) {
                    $dd++;       # I think we need this here to go one line after another
                }
                $string =~ s/->/$\to$/g;
            }
        }
        print "\n";
    }

while ループ内の最も内側の疑似コードについてはよくわかりません。

        while (<$fh>) {
            if (/\\begin{verbatim}/) {                    
                # Skip the Verbatim environment # TODO how this?
                # Need to tell it that you one line by one until \end{verbatim} is met, after which go normally forward.
                while (!/\\end{verbatim}/) {
                    $dd++;
                }
                $dd++;
            }
            else {
                if ($dd==0) {
                    $dd++;       # I think we need this here to go one line after another
                }
                $string =~ s/->/$\to$/g;
            }
        }

条件付きのこのような疑似コードをどのように記述すればよいでしょうか?

答え1

シェルが bash であると仮定すると、これは 1 行で実行できます。

perl -i.bak  -pe '
    /\\begin\{verbatim\}/../\\end\{verbatim\}/ or s/->/\$\\to\$/g
' {Cardiology,Pathophysiology,"Patology and Biopsy",Physiology,Propedeutics,Radiology,Rheumatology,Surgery}/*.tex

{...}は正規表現の量指定子なので、中括弧をエスケープする必要があることに注意してください。


コードは次のように書きます:

my @directories=(
    "Cardiology", "Pathophysiology", "Patology and Biopsy", "Physiology", 
    "Propedeutics", "Radiology", "Rheumatology", "Surgery"
);
chdir $path or die "cannot chdir '$path'";

foreach my $dir (@directories) {
    opendir my $DIR, $dir or die "cannot opendir '$dir'";
    while (my $file = readdir($DIR)) {
        my $filepath = "$dir/$file";
        next unless -f $filepath and $filepath =~ /\.tex$/;

        open my $f_in, "<", $filepath 
            or die "cannot open '$filepath' for reading";
        open my $f_out, ">", "$filepath.new"
            or die "cannot open '$filepath.new' for writing";

        while (<$fh>) {
            if (not /\\begin\{verbatim\}/ .. /\\end\{verbatim\}/) {                    
                s/->/\$\\to\$/g;
            }
            print $f_out;
        }

        close $f_in   or die "cannot close '$filepath'";
        close $f_out  or die "cannot close '$filepath.new'";

        rename $filepath, "$filepath.bak"  
            or die "cannot rename '$filepath' to '$filepath.bak'";
        rename "$filepath.new", $filepath
            or die "cannot rename '$filepath.new' to '$filepath'";
    }
    closedir $DIR  or die "cannot closedir '$dir'";
}

私はそれをさらにOOにしていきます:

use autodie qw(:io);
use Path::Class;

foreach my $dir (
        "Cardiology", "Pathophysiology", "Patology and Biopsy", "Physiology", 
        "Propedeutics", "Radiology", "Rheumatology", "Surgery"
) 
{
    my $directory = dir($path, $dir);
    while (my $file = $directory->next) {
        next unless -f $file and $file =~ /\.tex$/;
        my $f_out = file("$file.new")->open('w');
        for ($file->slurp) {
            /\\begin\{verbatim\}/ .. /\\end\{verbatim\}/  or s/->/\$\\to\$/g;
            $f_out->print;
        }
        $f_out->close;
        rename "$file", "$file.bak";
        rename "$file.new", "$file";
    }
}

関連情報