You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
3.6KB

  1. #!/usr/bin/perl
  2. # treesed
  3. # Written January 1996 by Rick Jansen (rick@sara.nl)
  4. # URL: http://www.sara.nl/rick
  5. # usage: treesed pattern1 pattern2 -tree
  6. # treesed pattern1 pattern2 -files file1 file2 ...
  7. # example: treesed href HREF -files *.html
  8. # Treesed searches for pattern1 and replaces pattern1 by pattern2
  9. # if pattern2 supplied. If only pattern1 given treesed just searches.
  10. # Treesed will search in all files and subdirectories of the current
  11. # directory
  12. #--------------------------------------------------------
  13. # Parameters
  14. $DoEdit=0;
  15. $search_pattern = $ARGV[0];
  16. $search_pattern =~ s/(\W)/\\$1/g; # escape regexp chars
  17. shift;
  18. while ($#ARGV >= 0) {
  19. if ($ARGV[0] eq '-files') {
  20. @temp_ls = @ARGV[1 .. $#ARGV];
  21. # Get list of files, skip dirs
  22. foreach $file (@ARGV[1 .. $#ARGV]) {
  23. if (-f $file) {
  24. push(@ls, $file);
  25. }
  26. }
  27. last;
  28. }
  29. elsif ($ARGV[0] eq '-tree') {
  30. &Get_LS;
  31. last;
  32. }
  33. if (! -f $ARGV[0]) {
  34. if (defined($replacement_pattern)) {
  35. print "usage: treesed pattern1 <pattern2> -tree/-files <files>\n";
  36. exit(1);
  37. }
  38. $replacement_pattern = $ARGV[0];
  39. #$replacement_pattern =~ s/(\W)/\\$1/g; # escape regexp chars
  40. $DoEdit=1;
  41. shift;
  42. }
  43. }
  44. # No files?
  45. if ($#ls < 0) {
  46. print "xx No input files\n";
  47. exit(1);
  48. }
  49. print "search_pattern: $search_pattern\n";
  50. print "replacement_pattern: $replacement_pattern\n";
  51. if ($DoEdit) {
  52. print "\n** EDIT MODE!\n\n"; }
  53. else {
  54. print "\n** Search mode\n\n";
  55. }
  56. #foreach $file (@ls) {
  57. # print "$file \n";
  58. #}
  59. #--------------------------------------------------------
  60. # Search list of files for pattern
  61. $linepos=0;
  62. $| = 1; # Force flush after every write
  63. foreach $file (@ls) {
  64. #print "$file\n";
  65. print '.';
  66. $linepos++;
  67. if ($linepos > 50) {
  68. $linepos=0;
  69. print "\n";
  70. }
  71. if (!open(FILE, $file)) {
  72. print "\nCould not open $file\n";
  73. next;
  74. }
  75. $Found = 0;
  76. $Count = 0;
  77. $lineno = 0;
  78. @lines = ();
  79. while (<FILE>) {
  80. $lineno++;
  81. if (/$search_pattern/i) {
  82. #print;
  83. $Count++;
  84. $Found = 1;
  85. push(@lines, $lineno);
  86. }
  87. }
  88. close(FILE);
  89. if ($Found) {
  90. print "\n$file: $Count lines on: @lines\n";
  91. }
  92. if ($Found && $DoEdit) { &Edit($file); }
  93. }
  94. $| = 0;
  95. print "\n";
  96. exit(0);
  97. #--------------------------------------------------------
  98. # Edit file
  99. sub Edit {
  100. # Replace $ARGV[0] with $ARGV[1] in $file
  101. local($file) = @_;
  102. local($bakfile) = $file.'.'.$$;
  103. # First create backup
  104. open(FILE, $file) || die "Could not open $file for read\n";
  105. open(BAKFILE, ">$bakfile") || die "Could not open $bakfile for backup\n";
  106. while (<FILE>) {
  107. print BAKFILE;
  108. }
  109. close(BAKFILE);
  110. close(FILE);
  111. # Now replace $ARGV[0] by $ARGV[1] in the backupfile,
  112. # result into $file
  113. open(BAKFILE, $bakfile) || die "Could not open $bakfile for read\n";
  114. open(FILE,">$file") || die "Could not open $file for write\n";
  115. $Count=0;
  116. while (<BAKFILE>) {
  117. if (/$search_pattern/i) { $Count++; }
  118. s/$search_pattern/$replacement_pattern/gi;
  119. print FILE;
  120. }
  121. close(BAKFILE);
  122. close(FILE);
  123. print
  124. "\nReplaced $search_pattern by $replacement_pattern on $Count lines in $file\n";
  125. } #sub Edit
  126. #--------------------------------------------------------
  127. sub Get_LS {
  128. # Get a list of full path names into array @ls
  129. local(@localls)=`ls -R1`;
  130. local($item,$Dir);
  131. #print "localls: @localls\n";
  132. $Dir='';
  133. foreach $item (@localls) {
  134. #print "$item\n";
  135. if ($item =~ /:$/) {
  136. $Dir=$item;
  137. chop($Dir);
  138. $Dir =~ s/:$/\//;
  139. }
  140. else {
  141. chop($item);
  142. $item = $Dir.$item;
  143. if ($item !~ /^\s*$/) { push(@ls, $item); }
  144. }
  145. }
  146. @localls=();
  147. } # sub Get_LS