[英]Meaningful data from text file 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
請注意,這里我把條件留空了,為此我嘗試了一些東西,但是它並不是我想要的那樣
應該這樣做:
更新:
$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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.