Forum Discussion
How to use Power Query to parse a txt file
What do you mean under "messages" and which headers you'd like to keep? Perhaps you may share sample file with the same information on screenshot.
- peiyezhuMay 01, 2023Bronze Contributorif possible,upload the txt file and show your expected result.
- anupambit1797May 02, 2023Iron Contributor
- peiyezhuMay 02, 2023Bronze Contributor
I can not figure out which specific table you want。
But I have tried to find one suitable and extracted for you.
I wonder if it is suitable for you with PHP.
But I think you can get the hints such like regular expression etc.
<?php $header="|Number|SCS |Number|Records|ID |RNTI Type |DCI Format |Agg Level |Prune Reason |Layers|K0|Offset|Symbol|ID |Ind |Type|ID |ID |Alloc |Alloc|Mapping|Error |Ind |Ind |MCS|RV|NDI|MCS1|RV1|NDI1|ID |CBGTI|CBGFI|Ack |Index|Group|Ports |Bundling|RMI|CSI|Init|TCI|Ind |FB |DAI|PUCCH|Req|Offset |MISC2 |"; //$line="| 2| 15KHZ| 509| 1| 0| C_RNTI| DL_1_1| LEVEL 4| NOT PRUNED| 1| 0| 4| 0| 1| 0| 0| 1| 1| 512| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 5| 0| 0| 0| 0| 0| 3| 0| 0| 0| 0| 0| 0| 0| 0| 1| 0| 0| 0|"; $files=glob('*.txt'); $handleOut=fopen('fileOut.csv','w'); echo fwrite($handleOut,str_replace('|',",",$header)."\n"); foreach($files as $filename){ $handle = fopen ($filename, "r"); $l=0; while (!feof ($handle)) { $buffer = fgets($handle, 4096); $line = trim($buffer); if(strlen($line)==337){ if(preg_match('{^\|\s*\d\|}',$line)){ echo fwrite($handleOut,str_replace('|',",",$line)."\n"); echo "\n"; $l++; } } // if($l>2) break; } fclose ($handle); } fclose($handleOut);
- SergeiBaklanMay 01, 2023Diamond Contributor
To connect to file as Text/CSV file, be sure that "|" is taken as delimiter. Remove first rows up to System info.
Next 3 rows will give headers, rest are content. Keep above table, reference on it, keep first 3 rows, transpose, merge all columns into new column, remove other columns, transpose back. That will be headers.
Reference initial table again and remove first 3 rows.
Combine second and third tables, use first row as header, declare types if necessary. That's the final one.
Again, it's better to illustrate on the sample file, above just an idea.
- anupambit1797May 02, 2023Iron ContributorIs it possible, we can have a session in Ms teams? If yes I can share my email