Forum Discussion
TEXT PARSING
- Oct 18, 2024
See PQ attached file. Not optimized for sure, just made sure it does the job...
(don't forget to change the file path in query 'CSV', it's currently mine)
Hi,
<?php
// Define the path to the log file and output CSV file
$logFilePath = '/storage/emulated/0/Download/SERVING SNR_UE-Log/SERVING SNR_UE-Log.txt';
$outputCsvPath = '/storage/emulated/0/Download/SERVING SNR_UE-Log/output.csv';
// Open the log file for reading
$logFile = fopen($logFilePath, 'r');
// Check if the file opened successfully
if (!$logFile) {
die('Unable to open log file: ' . $logFilePath);
}
// Prepare an array to hold the data for the CSV
$dataRows = [];
// Temporary variables to store the values we need
$currentTime = '';
$currentCellId = '';
$currentCarrierIndex = '';
$currentReferenceSignal = '';
$snrValues = [];
$rowId=0;
// Read the log file line by line
while (($line = fgets($logFile)) !== false) {
$rowId++;
// Check if the line contains the date (to capture time)
if (preg_match('/(\d{4} \w{3} \d{2} \s+(\d{2}:\d{2}:\d{2}\.\d{3}))/',$line, $matches)) {
$currentTime = $matches[2]; // Extract time
}
// Check for Cell Id
if (preg_match('/Cell Id = (\d+)/', $line, $matches)) {
$currentCellId = $matches[1];
}
// Check for Carrier Index
if (preg_match('/Carrier Index = (\d+)/', $line, $matches)) {
$currentCarrierIndex = $matches[1];
}
// Check for Reference Signal
if (preg_match('/Reference Signal = (\w+)/', $line, $matches)) {
$currentReferenceSignal = $matches[1];
}
// Check for SNR values
if (preg_match('/^\s*SNR = (.+) dB/', $line, $matches)) {
$snrValues[] = trim($matches[1]); // Collect SNR values
while (true) {
$line = fgets($logFile);
if($line===false) break;
$rowId++;
// echo $line;
if (empty(trim($line))){
break;
}
if (preg_match('/^\s*SNR = (.+) dB/', $line, $matches)) {
$snrValues[] = trim($matches[1]); // Collect SNR values
}
}
/*
echo 'rowid'.$rowId;
die;
*/
}
// If we reach the end of a block (next date line), save the current row
if (empty(trim($line)) && $currentTime) {
// Format the data for CSV
$dataRows[] = implode(',', [
$currentTime,
$currentCellId,
$currentCarrierIndex,
$currentReferenceSignal,
implode(',', $snrValues)
]);
// var_dump($dataRows);
// die;
// Reset temporary values for the next block
$currentTime = '';
$currentCellId = '';
$currentCarrierIndex = '';
$currentReferenceSignal = '';
$snrValues = [];
}
}
// Close the log file
fclose($logFile);
// Write the data to the CSV file
$outputCsv = fopen($outputCsvPath, 'w');
if (!$outputCsv) {
die('Unable to open output CSV file: ' . $outputCsvPath);
}
// Write each row to the CSV
foreach ($dataRows as $row) {
fputcsv($outputCsv, explode(',', $row));
}
// Close the output CSV file
fclose($outputCsv);
echo "Data has been successfully extracted to $outputCsvPath.";
?>