提取部分txt文件

时间:2018-05-12 10:20:29

标签: powershell extract

我有一个非常大的文本文件,我想提取以特定模式开头的文本文件的特定列到xls文件。我想要提取的数据以轨道ID号开始(即轨道ID = 20)。我希望能够读取轨道ID号,然后存储下面给出的一些信息,然后再次为下一个轨道ID等做。我在网上搜索并认为应该可以在Windows上使用PowerShell。

图片下方是文字的一小部分。在这种情况下,我想保留13号的ID号以及Z(mm)和KinE(MeV)列。

* G4Track Information:   Particle = proton,   Track ID = 13,   Parent ID = 0
*********************************************************************************************************

Step#    X(mm)    Y(mm)    Z(mm) KinE(MeV)  dE(MeV) StepLeng TrackLeng  NextVolume ProcName
    0        0        0 1.08e+03      100         0        0         0       World initStep
    1        0        0      800      100     3e-23      280       280     Tracker Transportation
    2        0        0      450      100  3.75e-23      350       630        Film StepLimiter    
    3        0        0      438     87.1      12.9       12       642        Film hIoni  
    4   -0.204   -0.136      429     76.7      10.4     9.42       651        Film hIoni
    5   -0.422   -0.104      421     67.3      9.43     7.53       659        Film hIoni
    6   -0.679  -0.0754      415     59.3      7.94     5.98       665        Film hIoni
    7   -0.899   -0.128      410     52.3      7.05     4.81       670        Film hIoni
    8    -1.01   -0.141      406     45.7       6.6     3.86       674        Film hIoni
    9    -1.04   -0.168      403     40.3      5.39     3.06       677        Film hIoni
   10     -1.1   -0.203      401     35.3      4.96     2.47       679        Film hIoni
   11    -1.13   -0.218      399     30.6      4.65     1.98       681        Film hIoni
   12    -1.16   -0.191      397     26.6      4.03     1.57       683        Film hIoni
   13    -1.21   -0.154      396     23.2       3.4     1.25       684        Film hIoni
   14    -1.26   -0.141      395       20      3.21     1.01       685        Film hIoni
   15     -1.3   -0.129      394     17.1       2.9    0.812       686        Film hIoni
   16    -1.34   -0.115      394     14.4      2.74    0.651       686        Film hIoni
   17    -1.31   -0.108      393     12.1      2.32    0.517       687        Film hIoni
   18    -1.29    -0.12      393     9.74      2.32    0.418       687        Film hIoni
   19    -1.28    -0.13      392     7.56      2.18    0.332       688        Film hIoni
   20    -1.27   -0.142      392     5.41      2.15    0.261       688        Film hIoni
   21    -1.27   -0.152      392     3.07      2.34    0.199       688        Film hIoni
   22    -1.28   -0.156      392    0.285      2.78    0.118       688        Film hIoni
   23    -1.28   -0.157      392        0     0.285  0.00319       688        Film hIoni

*********************************************************************************************************

part of the txt file

$log = Get-Content C:\Users\kpapa\Desktop\kkk.txt
foreach ($line in $log) { 
    if ($line -like "*878*") { 
        $line | Out-File -FilePath "C:\Users\kpapa\Desktop\kkkk.xls" -Append
    }
}

3 个答案:

答案 0 :(得分:0)

尝试这样的事情:

tensorflowjs_converter --input_format keras \
                       path/to/my_model.h5 \
                       path/to/tfjs_target_dir

-bash: tensorflowjs_converter: command not found

答案 1 :(得分:0)

This collects the needed data (Track ID, Z(mm), KinE(MeV)) and put it into a new Excel worksheet:

$filePath = 'C:\Users\kpapa\Desktop\kkk.txt'

$excelArray = New-Object 'object[,]' 1048576, 3     # 1048576 = maximum Excel rows
$row = 0                                            # row counter

foreach($line in [System.IO.File]::ReadLines($filePath) ) {

    # Track-Line => Get Track ID
    if($line -like '*Track ID*') {
       $trackID = ($line -split ',' | ? { $_ -like '*Track ID*' }).ToString() -replace '[^0-9]',''
    }
    else {
        # Value row? => Get Z(mm) and KinE(MeV)
        $tmpArray = $line -split ' ' | ? { $_ }
        if( $tmpArray.Length -gt 5 -and ( $tmpArray[0] -replace '[^0-9]' ).Length -gt 0 ) {
            $excelArray[$row,0] = $trackID
            $excelArray[$row,1] = [decimal]$tmpArray[3]   # Z(mm)
            $excelArray[$row,2] = [decimal]$tmpArray[4]   #KinE(MeV)
            $row++
        }
    }
}

# Put data to Excel...
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$workbook = $excel.Workbooks.Add()
$workbook.WorkSheets.Item(1).Range('A1:C' + $row.ToString() ).Value2 = $excelArray

答案 2 :(得分:0)

如果您左对齐NextVolume,则可以使用ConvertFrom-SourceTable cmdlet

$Particle = ConvertFrom-SourceTable '
Step#    X(mm)    Y(mm)    Z(mm) KinE(MeV)  dE(MeV) StepLeng TrackLeng  NextVolume ProcName
    0        0        0 1.08e+03      100         0        0         0  World      initStep
    1        0        0      800      100     3e-23      280       280  Tracker    Transportation
    2        0        0      450      100  3.75e-23      350       630  Film       StepLimiter    
    3        0        0      438     87.1      12.9       12       642  Film       hIoni  
    4   -0.204   -0.136      429     76.7      10.4     9.42       651  Film       hIoni
    5   -0.422   -0.104      421     67.3      9.43     7.53       659  Film       hIoni
    6   -0.679  -0.0754      415     59.3      7.94     5.98       665  Film       hIoni
    7   -0.899   -0.128      410     52.3      7.05     4.81       670  Film       hIoni
    8    -1.01   -0.141      406     45.7       6.6     3.86       674  Film       hIoni
    9    -1.04   -0.168      403     40.3      5.39     3.06       677  Film       hIoni
   10     -1.1   -0.203      401     35.3      4.96     2.47       679  Film       hIoni
   11    -1.13   -0.218      399     30.6      4.65     1.98       681  Film       hIoni
   12    -1.16   -0.191      397     26.6      4.03     1.57       683  Film       hIoni
   13    -1.21   -0.154      396     23.2       3.4     1.25       684  Film       hIoni
   14    -1.26   -0.141      395       20      3.21     1.01       685  Film       hIoni
   15     -1.3   -0.129      394     17.1       2.9    0.812       686  Film       hIoni
   16    -1.34   -0.115      394     14.4      2.74    0.651       686  Film       hIoni
   17    -1.31   -0.108      393     12.1      2.32    0.517       687  Film       hIoni
   18    -1.29    -0.12      393     9.74      2.32    0.418       687  Film       hIoni
   19    -1.28    -0.13      392     7.56      2.18    0.332       688  Film       hIoni
   20    -1.27   -0.142      392     5.41      2.15    0.261       688  Film       hIoni
   21    -1.27   -0.152      392     3.07      2.34    0.199       688  Film       hIoni
   22    -1.28   -0.156      392    0.285      2.78    0.118       688  Film       hIoni
   23    -1.28   -0.157      392        0     0.285  0.00319       688  Film       hIoni
'

PS C:\> $Particle | Where {$_.'Step#' -eq 20}

ProcName   : hIoni
Z(mm)      : 392
StepLeng   : 0.261
X(mm)      : -1.27
Y(mm)      : -0.142
dE(MeV)    : 2.15
KinE(MeV)  : 5.41
Step#      : 20
NextVolume : Film
TrackLeng  : 688

这适用于给定的示例,但我建议在标题下放置分隔符,因为如果所有字段的宽度与列标题相同,则可能无法确定列对齐,例如:

 X(mm)
------
 -0.12
 -0.23

详见:ConvertFrom-SourceTable -?

ConvertFrom-SourceTable cmdlet可在PowerShell库中下载,而GitHub iRon7/ConvertFrom-SourceTable存储库中可以找到源代码。