#!/usr/bin/perl # # by dennisg # # licensed under GPL # you could consider that a joke for such a small script my $table; my $difile; my $output; my $csv; my $file; my $new; my @search=(0); my @replace=(0); my $i; my $n; my $k; my $empty; if ( !$ARGV[0] || !$ARGV[1] ) { die "usage: search-and-replace.pl replace-with-these.csv file-in-which-replacements-are-made.any\n the new file will have the same name as the file to be replaced, just with a .new ending"; } elsif ( $ARGV[0] && $ARGV[1] ) { $table = $ARGV[0]; $difile = $ARGV[1]; $output = $difile.".new"; } open($file, $difile); open($csv, $table); open($new, '>'.$output); while (<$csv>) { ($search[$i], $replace[$i], $empty) = split(/;/, $_, 4); chomp($search[$i]); #$search[$i] =~ s/^\"(=.*)\"$/$1/g; chomp($replace[$i]); #$replace[$i] =~ s/^\"(=.*)\"$/$1/g; $i++; } close($csv); print "This could take some time..\n"; foreach $n (<$file>) { for ($k=0; $k<=$#search; $k++) { $n =~ s/\Q$search[$k]/$replace[$k]/g; } print $new $n; } print ".. done!\n"; close($new); close($file); exit 1;