来自文本文件powershell的有意义的数据

时间:2012-07-06 15:06:37

标签: windows powershell

我有一个类似于下面的文本文件,尝试通过使用通配符替换来获取有意义的数据,但不能完全正确。

 computer  - server1
 Volume 1         System Rese  NTFS  junk data
 Volume 2     C                NTFS  junk data 
 Volume 3         R5T1_ABCDEF  NTFS    junk data
   H:\R5X1_ABCDEF99XY2_APP01_ABCD_LH\
 Volume 4     H   R5T1_ABCDEF  NTFS       junk data
 Volume 5         R5T1_ABCDEF  NTFS   junk data
   H:\R5X1_ABCDEF99XY2_DBE01_EFGH_LH\
 Volume 10         R6T3_ABCDEF  NTFS  junk data
   H:\R6X3_ABCDEF99XY2_QRS_IJKL_LH\
 Volume 7         R5T2_ABCDEF  NTFS   junk data
   H:\R5X2_ABCDEF99XY2_QWE__MNOP_LH\
 Volume 8         R5T1_ABCDEF  NTFS   junk data
   H:\R5X1_ABCDEF99XY2_BTE___0DF8_LH\
 computer  - server2
 Volume 1         System Rese  NTFS  junk data
 Volume 2     C                NTFS  junk data 
 Volume 3         R5T1_ABCDEF  NTFS    junk data
   H:\R5X1_ABCDEF88XY2_APP01_ABCD_LH\
 Volume 4     H   R5T1_ABCDEF  NTFS       junk data
 Volume 5         R5T1_ABCDEF  NTFS   junk data
   H:\R5X1_ABCDEF88XY2_DBE01_EFGH_LH\
 Volume 10         R6T3_ABCDEF  NTFS  junk data
   H:\R6X3_ABCDEF88XY2_QRS_IJKL_LH\
 Volume 7         R5T2_ABCDEF  NTFS   junk data
   H:\R5X2_ABCDEF88XY2_QWE__MNOP_LH\
 Volume 8         R5T1_ABCDEF  NTFS   junk data
   H:\R5X1_ABCDEF88XY2_BTE___0DF8_LH\

这是我正在寻找的输出:1)获取那些旁边有一个字母的卷(如2,4卷)。 2)获取那些旁边没有字母的卷,它下面的行不是卷线(如3,5,6卷)。 3)删除那些没有字母或下面没有非体积线的卷(如第1卷)。

最终输出如下:

computer1 Volume 2  C
computer1 Volume 3  H:\R5X1_ABCDEF99XY2_APP01_ABCD_LH\
computer1 Volume 4  H
computer1 Volume 5  H:\R5X1_ABCDEF99XY2_DBE01_EFGH_LH\
computer1 Volume 10 H:\R6X3_ABCDEF99XY2_QRS_IJKL_LH\
computer2 Volume 2  C
computer2 Volume 3  H:\R5X1_ABCDEF88XY2_APP01_ABCD_LH\
computer2 Volume 2  H
computer2 Volume 3  H:\R5X1_ABCDEF88XY2_DBE01_EFGH_LH\
computer2 Volume 4  H:\R6X3_ABCDEF88XY2_QRS_IJKL_LH\
computer2 Volume 10 H:\R6X3_ABCDEF88XY2_QRS_IJKL_LH\

修改评论示例代码:

我有点卡在需要使用的条件上:

$FileListArray2 = @()
Foreach($file in Get-Content $FilesName | Where-Object {$_ -notmatch "(junk1)|(junk2)"}) { 
    if($file -match "(Volume)") { } 
    $FileListArray2 += ,@($file2) 
} 
$FileListArray2 

请注意,这里我已经将条件位置空了,我已经尝试了一些东西,但它的工作方式并不像我想要的那样

1 个答案:

答案 0 :(得分:0)

这应该这样做:

<强>更新:

$inputstring = @'
 computer  - server1
 Volume 1         System Rese  NTFS  junk data
 Volume 2     C                NTFS  junk data 
 Volume 3         R5T1_ABCDEF  NTFS    junk data
   H:\R5X1_ABCDEF99XY2_APP01_ABCD_LH\
 Volume 4     H   R5T1_ABCDEF  NTFS       junk data
 Volume 5         R5T1_ABCDEF  NTFS   junk data
   H:\R5X1_ABCDEF99XY2_DBE01_EFGH_LH\
 Volume 10         R6T3_ABCDEF  NTFS  junk data
   H:\R6X3_ABCDEF99XY2_QRS_IJKL_LH\
 Volume 7         R5T2_ABCDEF  NTFS   junk data
   H:\R5X2_ABCDEF99XY2_QWE__MNOP_LH\
 Volume 8         R5T1_ABCDEF  NTFS   junk data
   H:\R5X1_ABCDEF99XY2_BTE___0DF8_LH\
 computer  - server2
 Volume 1         System Rese  NTFS  junk data
 Volume 2     C                NTFS  junk data 
 Volume 3         R5T1_ABCDEF  NTFS    junk data
   H:\R5X1_ABCDEF88XY2_APP01_ABCD_LH\
 Volume 4     H   R5T1_ABCDEF  NTFS       junk data
 Volume 5         R5T1_ABCDEF  NTFS   junk data
   H:\R5X1_ABCDEF88XY2_DBE01_EFGH_LH\
 Volume 10         R6T3_ABCDEF  NTFS  junk data
   H:\R6X3_ABCDEF88XY2_QRS_IJKL_LH\
 Volume 7         R5T2_ABCDEF  NTFS   junk data
   H:\R5X2_ABCDEF88XY2_QWE__MNOP_LH\
 Volume 8         R5T1_ABCDEF  NTFS   junk data
   H:\R5X1_ABCDEF88XY2_BTE___0DF8_LH\
'@

# Split the input into an array of strings
$lines = $inputstring -split '[\r\n]'

# Setup patterns to match against
$pattern1 = "(Volume \d+)\s+(\w)\s+"
$pattern2 = "(Volume \d+)\s+"
$pattern3 = "\s+(\w:\\.*)"
$pattern4 = "^ computer  - (\S+)$"

# Store the current computer that is being viewed
$currentcomputer = "UNKNOWN"

# Loop through each line
for($i = 0; $i -lt $lines.count; $i++)
{
    # Start off assuming the current line produces no output
    $output = ""

    # Look for volumes with drive letters
    if($lines[$i] -match $pattern1)
    {
        $output = $matches[1] + " " + $matches[2]
    }
    # Look for volumes without drive letters
    elseif($lines[$i] -match $pattern2)
    {
        $volume = $matches[1]

        # Consider the next line to see if it has a path
        if($lines[$i + 1] -match $pattern3)
        {
            $output = $volume + " " + $matches[1]

            # If the next line had a path then we handled it and need to skip it
            $i++
        }
    }
    elseif($lines[$i] -match $pattern4)
    {
        $currentcomputer = $matches[1]
    }

    # Write out any output that was produced
    if($output -ne "")
    {
        $currentcomputer + " " + $output
    }
}