Home > Coding Tips, vim > Using Perl for Complex Edits

Using Perl for Complex Edits

All I use Vim 99% of the time whenever I want to do a really complex multiple edit I tend to use Perl.

So what’s the script doing ? It reads in one text file line by line and then operates on certain lines , and then writes out each line to an output file. You are only limited by your imagination as to what you do in the loop.

With a Perl script you can use Perl variables for counts, you can use a hash to store all the text and loop through it as often as required.

# pedit.pl
# description : Use for complex edits on a single file, adapt as required
# this trivial example is just incrementing a count
# zzapper
# v1.0
if ($#ARGV >= 0) { $file = $ARGV[0]; }
print "enter file:"; $file=;
chomp $file;
open (R,"$file") or die ("could not open $file ($!)");
$out = "c:/aaa/out.txt";
$out = "$file.txt";

open (W,">$out") or die ("could not open $out ($!)",__LINE__);
for $l ()
$l=~s/david1/david$cnt/gi; # do what you want here
print W $l;
close W; close R;
print "\nOutput in cnt: $cnt :: $out\n";

Categories: Coding Tips, vim Tags:
  1. No comments yet.
  1. No trackbacks yet.