lddzjwwy has asked for the wisdom of the Perl Monks concerning the following question:

Hey all, how can I use perl to do substitution for all files of one folder in one go? E.g., to substitute <> in all markdown files into `<>`. Thanks a lot for your kind help. BR Wei

Replies are listed 'Best First'.
Re: Perl substitute
by choroba (Cardinal) on Apr 15, 2013 at 12:13 UTC
    perl -i~ -pe 's/<>/`<>`/g' directory/*
    See perlrun, perlop.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Perl substitute
by Don Coyote (Hermit) on Apr 15, 2013 at 13:52 UTC

    This will do what you need, however, you will need to adapt the untainting regex to your particular requirements. I was a little uncertain about processing a regex with backticks, so I used taintmode.

    #!usr/bin/perl -Tw use warnings; use strict; opendir my $dh, './testdir/'; #assume all files, no sub dirs my @filelist = readdir($dh); close($dh); my $rep = qr'<>'; while (@filelist){ my $basefile = shift @filelist; next unless $basefile =~ m/^((\w+\s*)+\.(pl|txt))$/; $basefile = $1; my $pathfile = './testdir/'.$basefile; open (my $rfh, '<', $pathfile); my @filelines; while (<$rfh>){ $_ =~ s/$rep/\`<>\`/g; push @filelines, $_; } close($rfh); open (my $wfh, '>', $pathfile); while (@filelines){ my $line = shift @filelines; print $wfh $line; } close($wfh); } exit 0;