Posted By

noah on 01/28/07


Tagged

file text batch


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

fsroque
icecreamboyy


Text change across multiple files


 / Published in: Perl
 

Put this in a script called "update.pl" and call it with "ls *ext|xargs perl update.pl" Be sure to back up the work directory before playing with this, as it is very easy to wipe out the content of a bunch of files at once in this way.

  1. #! /usr/bin/perl -w
  2. use strict;
  3. $^I=".bk";
  4. while (<>) {
  5. $_ =~ s#rico.js#NiftyCube/niftycube.js#;
  6. print $_;
  7. }
  8. #run it like this: ls *html|xargs perl update.pl
  9.  
  10.  
  11.  
  12. #Here are some other versions of the same kind of thing:
  13. #First version:
  14. #code snippet to search-and-replace a group of files...
  15. $^I=".bk"; # Create a backup
  16. while (<>) {
  17. s/'/'/gi;
  18. print $_ ;
  19. }
  20.  
  21. #Second variation: read from a list of files
  22. #Assumes that name of list_file is first command line argument
  23. #Also assumes we are reading a file with one URL on each line
  24. while (<>) {
  25. chomp(my $url = $_);
  26. if ($url ne "") {
  27. grabit($_, $tag_type, $local_directory, $extensions);
  28. print $url . "\n";
  29.  
  30. }
  31. }
  32.  
  33.  
  34. #Third variation: read a whole file into an array
  35. my $input_file= shift;
  36. open(WHOLEFILE_HANDLE, $input_file) || die("Couldn't open $input_file\n");
  37. my @whole_file=<WHOLEFILE_HANDLE>;
  38. foreach my $i (@whole_file) {
  39. print "$i";
  40. }

Report this snippet  

You need to login to post a comment.