Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for far.pl

(view source code of far.pl as plain text)

  1. #! perl
  2.  
  3. # Check command line arguments
  4. if ( !@ARGV[2] ) {
  5. 	print "\nFaR -- Find and Replace,  Version 1.01\n";
  6. 	print "Search a string for a substring and replace it with another substring\n\n";
  7. 	print "Usage:  FAR.PL  \"string0\"  \"string1\"  \"string2\"  [ -I ]\n\n";
  8. 	print "Where:  \"string0\" is the string to be searched\n";
  9. 	print "        \"string1\" is the substring to search for\n";
  10. 	print "        \"string2\" is the substring that should replace string1\n";
  11. 	print "        -I        makes the search case insensitive\n\n";
  12. 	print "All strings must be enclosed in double quotes and \"escaped\".\n\n";
  13. 	print "Written by Rob van der Woude\n";
  14. 	print "http://www.robvanderwoude.com\n";
  15. 	exit(1);
  16. }
  17.  
  18. # Parse command line arguments
  19. $string0 = @ARGV[0];
  20. $string1 = @ARGV[1];
  21. $string2 = @ARGV[2];
  22. $caseon  = "";
  23. $caseoff = "";
  24. if ( @ARGV[3] ) {
  25. 	if ( uc( @ARGV[3] ) eq "-I" ) {
  26. 		$caseon  = "(?i)";
  27. 		$caseoff = "(?-i)";
  28. 	}
  29. }
  30.  
  31. # Search and replace using a regular expression
  32. $string0 =~ s/$caseon$string1$caseoff/$string2/g;
  33.  
  34. # Display the end result
  35. print "$string0\n";
  36.  

page last uploaded: 2017-08-21, 14:26