0

我有一个如下所示的文本文件,尝试通过替换、使用通配符来获取有意义的数据,但并不完全正确。

 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 

请注意,在这里我将条件留空,我为此尝试了一些东西,但它并没有按照我想要的方式工作

4

1 回答 1

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
    }
}
于 2012-07-07T16:06:18.877 回答