2017-07-10 66 views
1

我想要屏蔽字符串中的字段,如下所示。它在一定程度上工作,實際上是一半。在$addresspostcode之後的某個階段,替換字符未替換爲正確的位置。會有人有一個解決的想法?掩蔽字段複製

的adressee0線從輸出文件

 
ADDRESSEE0|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY|YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY|YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY|YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY|ZZZZZZZZ|Sir or MadamZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ  |A1|OM|Mr Patrick MurphyZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|45 CregtownZZZZZZZZZZZZZZZZ |EastRoad RoadZZZZZZZZZZZZZZZZ |TownnamersZZZZZZZZZZZZZZZZ |CityAB 16ZZZZZZZZZZZZZZZZ  |ZZZZZZZZ| |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|ZZZZZZZZZZZZZZZZ |Sir or MadamZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ  |IA|3319041|  |  |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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ZZZZ 
ForEach-Object { 
    $addresseeName = $_.Substring(11,50) 
    $addresseeName2 = $_.Substring(62,50) 
    $addresseeLine1 = $_.Substring(113,30) 
    $addresseeLine2 = $_.Substring(144,30) 
    $addresseeLine3 = $_.Substring(175,30) 
    $addresseeLine4 = $_.Substring(206,30) 
    $addresseePostCode = $_.Substring(237,8) 
    $referenceAddressName1 = $_.Substring(303,50) 
    $referenceAddressName2 = $_.Substring(354,50) 
    $referenceAddresseeLine1 = $_.Substring(405,30) 
    $referenceAddresseeLine2 = $_.Substring(436,30) 
    $referenceAddresseeLine3 = $_.Substring(467,30) 
    $referenceAddresseeLine4 = $_.Substring(498,30) 

    $mask50 = 'X' * 50 
    $mask30 = 'Y' * 30 
    $mask08 = 'Z' * 8 

    # IF statement, if the string is at position 0-10, and begins with 'ADDRESSEE0' 
    # then run replace statement 
    if ($_.Substring(0,10) -eq 'ADDRESSEE0') { 
    $_.Replace($addresseeName, $mask50).Replace($addresseeName2, $mask50).Replace($addresseeLine1, $mask30).Replace($addresseeLine2, $mask30).Replace($addresseeLine3, $mask30).Replace($addresseeLine4, $mask30).Replace($addresseePostCode, $mask08).Replace($referenceAddressName1, $mask50).Replace($referenceAddressName2, $mask50).Replace($referenceAddresseeLine1, $mask30).Replace($referenceAddresseeLine2, $mask30).Replace($referenceAddresseeLine3, $mask30).Replace($referenceAddresseeLine4, $mask30) 

回答

3

你的代碼的問題是它根據字符串的內容(而不是位置)進行替換。這意味着如果相同的文本存在於字符串的其他地方,它也會替換該文本,從而取代後者。

我建議你這樣做,而不是:

$mask50 = 'X' * 50 
$mask30 = 'Y' * 30 
$mask08 = 'Z' * 8 

$SomeInput | ForEach-Object { 

    if ($_.Substring(0,10) -eq 'ADDRESSEE0') 
    { 
     $SplitString = $_.Split('|') 
     1..2 | ForEach-Object { $SplitString[$_] = $mask50 } 
     3..6 | ForEach-Object { $SplitString[$_] = $mask30 } 
     $SplitString[7] = $mask08 
     8..9 | ForEach-Object { $SplitString[$_] = $mask50 } 
     10..13 | ForEach-Object { $SplitString[$_] = $mask30 } 

     $SplitString -Join '|' 
    } 
} 

此分裂基礎上,|字符的字符串,然後做個人取代了各(我們使用的..數組表示法,使這一點更有效) 。

然後我們再次以字符|結尾字符串。

+1

這很棒,比我做的要容易得多。 所以1..2是由'|'分隔的第一個字段所以我所做的是 $SplitString[7] = $mask08 \t \t 11..12 | ForEach-Object { $SplitString[$_] = $mask50 } 作爲字段(8,9,10)不需要被屏蔽。謝謝馬克! – conye9980

3

假設ADDRESSEE0|...是輸入字符串,爲什麼不第一分割數據?這提供了可管理的塊,而不是一大串卡車方法鏈。像這樣,

# Get input data 
$raw = `'ADDRESSEE0|ADDRESSEE1|ADDRESSEELine0|ADDRESSEELine1|...'` 

# Split the string by each pipe | char. This uses regex syntax, so escape \ is needed 
$lines = $raw -split '\|' 

# Assign splitted elements into more readable variables 
$addresseeName = $lines[0] 
$addresseeName2 = $lines[1] 
$addresseeLine1 = $lines[2] 
... 

# mask the data whatever way floats your boat 
$addresseeName = $addresseeName.substring(0,9) + $mask08 
+0

感謝您的評論,斯普利特似乎是要走的路。 – conye9980