<?php
/**
* CLI tool to replace values in log streams.
*
* Usage: php replace_log_values.php <log_file> <search_string> <replace_string>
*/
if (count($argv) !== 4) {
echo "Usage: php replace_log_values.php <log_file> <search_string> <replace_string>\n";
exit(1);
}
$logFile = $argv[1];
$searchString = $argv[2];
$replaceString = $argv[3];
if (!file_exists($logFile)) {
echo "Error: Log file '$logFile' not found.\n";
exit(1);
}
$logContent = file_get_contents($logFile);
if ($logContent === false) {
echo "Error: Could not read log file '$logFile'.\n";
exit(1);
}
$newLogContent = str_replace($searchString, $replaceString, $logContent);
if ($newLogContent === $logContent) {
echo "No matches found for '$searchString'.\n";
exit(0);
}
file_put_contents($logFile, $newLogContent);
echo "Successfully replaced '$searchString' with '$replaceString' in '$logFile'.\n";
exit(0);
?>
Add your comment