【问题标题】:String split with PowerShell使用 PowerShell 拆分字符串
【发布时间】:2014-04-02 23:27:07
【问题描述】:

我有一个包含 S.M.A.R.T. 的日志文件。我的硬盘数据。 我想用 PowerShell 处理这个文件。 这是我的日志文件的一部分。

3 Spin_Up_Time            0x0020   100   100   000    Old_age   Offline      -       0
4 Start_Stop_Count        0x0030   100   100   000    Old_age   Offline      -       0
5 Reallocated_Sector_Ct   0x0032   100   100   000    Old_age   Always       -       0

这是我的代码

$i = 1
$a = Get-Content log.txt

do {
$trimmed = $a[$i].trim()
$splitted = $trimmed.split(" ")
$i++
}while ($i -le 3)

如果我使用 .split(" ") 它仅适用于第三行。 如何正确拆分所有行?

谢谢

【问题讨论】:

  • 如果我将 $splitted[2] 放在循环结束之前,我将无法获得预期值。(0x0020 0x0030 0x0032)

标签: powershell


【解决方案1】:

更多的代码,但它最终为您提供了一些更容易使用的东西:

$SMART = gc c:\temp\test.txt | %{
    $temp = $_ -split " "|?{!([string]::IsNullOrWhiteSpace($_))}
    new-object psobject -Property @{
        "Entry"=$temp[0]
        "TestName"=$temp[1]
        "HexCode"=$temp[2]
        "Number1"=$temp[3]
        "Number2"=$temp[4]
        "Number3"=$temp[5]
        "Age"=$temp[6]
        "Status"=$temp[7]
        "Filler"=$temp[8]
        "Zero?"=$temp[9]
    }
}
$SMART|FT Entry,TestName,HexCode,Number1,Number2,Number3,Age,Status,Filler,Zero?

【讨论】:

    【解决方案2】:

    这对你有什么好处?

    $a = Get-Content log.txt    
    -split $a
    

    我明白了

    H:\> -split $a
    3
    Spin_Up_Time
    0x0020
    100
    100
    000
    Old_age
    Offline
    -
    0
    4
    Start_Stop_Count
    0x0030
    100
    100
    000
    Old_age
    Offline
    -
    0
    5
    Reallocated_Sector_Ct
    0x0032
    100
    100
    000
    Old_age
    Always
    -
    0
    

    【讨论】:

      【解决方案3】:

      我喜欢使用正则表达式'这是一个示例,可让您命名列。

      $a = Get-Content log.txt
      $pattern = [regex]'(?<rowid>\d+)\s(?<desc>[a-zA-Z_]+)\s+(?<hexdata>0x\d{4})\s+(?<col4>\d{3})\s+(?<col5>\d{3})\s+(?<col6>\d{3})\s+(?<text1>.+?)\s+(?<state>.+?)-\s+0'
      
      foreach ($line in $a) {
          if ($line -match $pattern) {
              $dataobj = New-Object PSObject
              $dataobj | Add-Member -type NoteProperty -name "Description" -value $matches['desc']
              $dataobj | Add-Member -type NoteProperty -name "Hex Data" -value $matches['hexdata']
              $dataobj | Add-Member -type NoteProperty -name "State" -value $matches['state']
      
              $dataobj
          }
      
      }
      

      结果:

      Description                                          Hex Data                                             State                                              
      -----------                                          --------                                             -----                                              
      Spin_Up_Time                                         0x0020                                               Offline                                            
      Start_Stop_Count                                     0x0030                                               Offline                                            
      Reallocated_Sector_Ct                                0x0032                                               Always                                             
      

      【讨论】:

        猜你喜欢
        • 2015-02-07
        • 1970-01-01
        • 2019-12-16
        • 2013-09-26
        • 1970-01-01
        • 2018-08-08
        • 2020-02-07
        • 2014-03-08
        • 1970-01-01
        相关资源
        最近更新 更多