Forum Discussion
peiyezhu
Feb 19, 2024Bronze Contributor
About parse txt to
Re: https://techcommunity.microsoft.com/t5/excel/power-query-help/m-p/4059319#M220947 I can not open original thread with my smart phone so I open a new thread here anupambit1797 http:/...
peiyezhu
Feb 19, 2024Bronze Contributor
add time stamp
<?php
function parseBody(){
$files=glob('*.txt');
$handleOut=fopen('fileOut.csv','w');
$pattern='/Frame|RNTI Type|Excess Time|TB Ext 0|On Dur/';
foreach($files as $filename){
$handle = fopen ($filename, "r");
$new_line="";
$prefix="";
while (!feof ($handle))
{
$buffer = fgets($handle, 4096);
$line = trim($buffer);
//here add time stamp
if(preg_match('/1980 Jan/',$line,$m)){
if($prefix==""){
$prefix=$line;
$prefix_next=$line;
}else{
$prefix=$prefix_next;
$prefix_next=$line;
}
}
if(preg_match('/TB Info Record\[.*\]/',$line,$m)){
if($new_line<>""){
$new_line=str_replace(',,',',',$new_line);
$new_line=trim($new_line,',');
$new_line=$prefix.','.$new_line;
fwrite($handleOut,$new_line."\n");
$new_line=$m[0];
}else{
$new_line=$m[0];
}
}
if(preg_match($pattern,$line)){
$buffer = fgets($handle, 4096);
$buffer = fgets($handle, 4096);
$line = trim($buffer);
$new_line.=str_replace('|',",",$line);
}
}
$prefix=$prefix_next;
$new_line=str_replace(',,',',',$new_line);
$new_line=trim($new_line,',');
$new_line=$prefix.','.$new_line;
fwrite($handleOut,$new_line);
fclose ($handle);
}
fclose($handleOut);
}
parseBody();
- peiyezhuMar 01, 2024Bronze Contributor<?php
function handleArr($arr){
global $handleOut;
$new_line="";
$prefix="";
$write_line=false;
$pattern='/Frame|RNTI Type|Excess Time|TB Ext 0/';
$prefix=array_shift($arr);
for($i=0;$l=sizeof($arr),$i<$l;$i++){
$line = trim($arr[$i]);
if(preg_match('/TB Info Record\[.*\]/',$line,$m)){
$prefix2=$prefix.','.$m[0];
}
if(preg_match($pattern,$line)){
$i++;
$i++;
$line = trim($arr[$i]);
$new_line.=str_replace('|',",",$line);
}
if(preg_match('/On Dur/',$line)){
// echo substr($line,0,10);
// echo "\n";
$write_line=true;
}
if($write_line){
$i++;
$i++;
$line = trim($arr[$i]);
$new_line.=str_replace('|',",",$line);
$new_line=$prefix2.','.$new_line;
$new_line.="\n";
$new_line=str_replace(',,',',',$new_line);
fwrite($handleOut,$new_line);
$new_line="";
$write_line=false;
}
}
}
$files=glob('*.txt');
$handleOut=fopen('fileOut.csv','w');
foreach($files as $filename){
$handle = fopen ($filename, "r");
//first line should be 1980 Jan 12 04:29:31.127 [BB] 0xB132 LTE LL1 PDSCH Decoding Results
$buffer = fgets($handle, 4096);
$line = trim($buffer);
$arr[]=$line;
while (!feof($handle))
{
$buffer = fgets($handle, 4096);
$line = trim($buffer);
if(preg_match('/1980 Jan/',$line)){
//from second 1980 Jan
// print_r($arr);
handleArr($arr);
unset($arr);
}else{
}
$arr[]=$line;//reset $arr;
}
handleArr($arr);
fclose ($handle);
}
fclose($handleOut);
die;