1. <?php
  2. /**
  3. * CLI tool to replace values in log streams.
  4. *
  5. * Usage: php replace_log_values.php <log_file> <search_string> <replace_string>
  6. */
  7. if (count($argv) !== 4) {
  8. echo "Usage: php replace_log_values.php <log_file> <search_string> <replace_string>\n";
  9. exit(1);
  10. }
  11. $logFile = $argv[1];
  12. $searchString = $argv[2];
  13. $replaceString = $argv[3];
  14. if (!file_exists($logFile)) {
  15. echo "Error: Log file '$logFile' not found.\n";
  16. exit(1);
  17. }
  18. $logContent = file_get_contents($logFile);
  19. if ($logContent === false) {
  20. echo "Error: Could not read log file '$logFile'.\n";
  21. exit(1);
  22. }
  23. $newLogContent = str_replace($searchString, $replaceString, $logContent);
  24. if ($newLogContent === $logContent) {
  25. echo "No matches found for '$searchString'.\n";
  26. exit(0);
  27. }
  28. file_put_contents($logFile, $newLogContent);
  29. echo "Successfully replaced '$searchString' with '$replaceString' in '$logFile'.\n";
  30. exit(0);
  31. ?>

Add your comment