Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell-查找并替换多个模式以匿名化文件_Powershell_Replace_Find - Fatal编程技术网

PowerShell-查找并替换多个模式以匿名化文件

PowerShell-查找并替换多个模式以匿名化文件,powershell,replace,find,Powershell,Replace,Find,我需要你的帮助。我有一个log.txt文件,其中包含各种数据,我必须匿名。 我想检索所有这些与预定义模式匹配的“字符串”,并用它们各自的另一个值替换它们。重要的是,来自同一模式的每个新字符串(以及与前一模式不同的值)应替换为增加+1的预定义值(例如,“orderID=123ABC”变为“orderID=order1”,而“orderID=456ABC”变为“orderID=order2”)。 要搜索的模式超过20个,因此不可能将它们全部放在一行中。 我的想法是: 定义“patterns.txt”

我需要你的帮助。我有一个log.txt文件,其中包含各种数据,我必须匿名。 我想检索所有这些与预定义模式匹配的“字符串”,并用它们各自的另一个值替换它们。重要的是,来自同一模式的每个新字符串(以及与前一模式不同的值)应替换为增加+1的预定义值(例如,“orderID=123ABC”变为“orderID=order1”,而“orderID=456ABC”变为“orderID=order2”)。
要搜索的模式超过20个,因此不可能将它们全部放在一行中。 我的想法是:

  • 定义“patterns.txt”文件
  • 定义“replace.txt”文件(“模式”值和替换值)
  • 搜索日志文件中的所有“模式”,结果将是数组
  • 在该数组中查找唯一的条目
  • 获取数组中每个唯一项的“替换”值
  • 替换log.txt中的所有事件。这里棘手的部分是,任何相同类型(但与前一个不同的值)的出现都需要增加(+1),以便与之前的出现不同
  • 我所拥有的示例:


    requestID>qwerty1-qwerty2-qwerty312345a-12345b-12345cqwerty1-qwerty2-qwerty3012345ABCDE012345ABCDEABCDE012345ABCDE012345XYZ123ABC987XYZ123Request-1Request-1Request-1Order-1Order-1Order-1Order-2Order-2Order-2Order-2Key-1Key-2Key-2Key-2Key-1Key-1Key-1Key-1Key-1Key-1Key-1Key-1您的模式与示例数据不匹配。我已经纠正了模式,以适应实际的样本数据

    每个类型的简单哈希表似乎可以满足跟踪匹配和计数的需要。如果我们使用
    -Regex
    -file
    参数使用
    开关
    语句处理日志文件,我们可以一次处理每一行。每种方法的逻辑都是

    • 检查特定类型的匹配数组中是否存在当前匹配。
      • 如果不是,则将其与替换值(类型计数)和增量计数一起添加
      • 如果确实存在,请使用已定义的替换值
    • 捕获变量中的所有输出,完成后将其写入文件
    创建示例日志文件

    $log = New-TemporaryFile
    
    @'
    <requestID>qwerty1-qwerty2-qwerty3</requestID> -match 
    <requestID>12345a-12345b-12345c</requestID>
    <requestID>qwerty1-qwerty2-qwerty3</requestID>
    <requestID>qwerty1-qwerty2-qwerty3</requestID>
    <orderID>012345ABCDE</orderID>
    <orderID>012345ABCDE</orderID>
    <orderID>ABCDE012345</orderID>
    <orderID>ABCDE012345</orderID>
    <keyId>XYZ123</keyId>
    <keyId>ABC987</keyId>
    <keyId>XYZ123</keyId>
    '@ | Set-Content $log -Encoding UTF8
    
    $output = switch -Regex -File $log {
        '<requestID>(\w{6,7}-\w{6,7}-\w{6,7})</requestID>' {
            if(!$Request.matches.($matches.1))
            {
                $Request.matches += @{$matches.1 = "Request-$($Request.count)"}
                $Request.count++
            }
            $_ -replace $matches.1,$Request.matches.($matches.1)
        }
        '<orderID>(\w{11})</orderID>' {
            if(!$Order.matches.($matches.1))
            {
                $Order.matches += @{$matches.1 = "Order-$($Order.count)"}
                $Order.count++
            }
            $_ -replace $matches.1,$Order.matches.($matches.1)
        }
        '<keyId>(\w{6})</keyId>' {
            if(!$Key.matches.($matches.1))
            {
                $Key.matches += @{$matches.1 = "Key-$($Key.count)"}
                $Key.count++
            }
            $_ -replace $matches.1,$Key.matches.($matches.1)
        }
        default {$_}
    }
    
    $output | Set-Content $log -Encoding UTF8
    
    逐行读取并处理日志文件

    $log = New-TemporaryFile
    
    @'
    <requestID>qwerty1-qwerty2-qwerty3</requestID> -match 
    <requestID>12345a-12345b-12345c</requestID>
    <requestID>qwerty1-qwerty2-qwerty3</requestID>
    <requestID>qwerty1-qwerty2-qwerty3</requestID>
    <orderID>012345ABCDE</orderID>
    <orderID>012345ABCDE</orderID>
    <orderID>ABCDE012345</orderID>
    <orderID>ABCDE012345</orderID>
    <keyId>XYZ123</keyId>
    <keyId>ABC987</keyId>
    <keyId>XYZ123</keyId>
    '@ | Set-Content $log -Encoding UTF8
    
    $output = switch -Regex -File $log {
        '<requestID>(\w{6,7}-\w{6,7}-\w{6,7})</requestID>' {
            if(!$Request.matches.($matches.1))
            {
                $Request.matches += @{$matches.1 = "Request-$($Request.count)"}
                $Request.count++
            }
            $_ -replace $matches.1,$Request.matches.($matches.1)
        }
        '<orderID>(\w{11})</orderID>' {
            if(!$Order.matches.($matches.1))
            {
                $Order.matches += @{$matches.1 = "Order-$($Order.count)"}
                $Order.count++
            }
            $_ -replace $matches.1,$Order.matches.($matches.1)
        }
        '<keyId>(\w{6})</keyId>' {
            if(!$Key.matches.($matches.1))
            {
                $Key.matches += @{$matches.1 = "Key-$($Key.count)"}
                $Key.count++
            }
            $_ -replace $matches.1,$Key.matches.($matches.1)
        }
        default {$_}
    }
    
    $output | Set-Content $log -Encoding UTF8
    
    $output=switch-Regex-File$log{
    “(\w{6,7}-\w{6,7}-\w{6,7})”{
    如果(!$Request.matches.($matches.1))
    {
    $Request.matches+=@{$matches.1=“Request-$($Request.count)”}
    $Request.count++
    }
    $\替换$matches.1、$Request.matches.($matches.1)
    }
    “(\w{11})”{
    如果(!$Order.matches.($matches.1))
    {
    $Order.matches+=@{$matches.1=“Order-$($Order.count)”}
    $Order.count++
    }
    $\替换$matches.1、$Order.matches.($matches.1)
    }
    “(\w{6})”{
    如果(!$Key.matches.($matches.1))
    {
    $Key.matches+=@{$matches.1=“Key-$($Key.count)”}
    $Key.count++
    }
    $\替换$matches.1、$Key.matches.($matches.1)
    }
    默认值{$}
    }
    $output |设置内容$log-编码UTF8
    
    $log文件现在包含

    <requestID>Request-1</requestID>
    <requestID>Request-2</requestID>
    <requestID>Request-1</requestID>
    <requestID>Request-1</requestID>
    <orderID>Order-1</orderID>
    <orderID>Order-1</orderID>
    <orderID>Order-2</orderID>
    <orderID>Order-2</orderID>
    <keyId>Key-1</keyId>
    <keyId>Key-2</keyId>
    <keyId>Key-1</keyId>
    
    Request-1
    请求-2
    请求-1
    请求-1
    订单-1
    订单-1
    订单-2
    订单-2
    键-1
    键-2
    键-1
    
    谢谢道格,这正是我需要的。现在,作为进一步的步骤,如何在匿名过程中生成一个文件,其中包含原始数据和相应的匿名值(例如XYZ123 Key-1)?在这个文件/表格的帮助下,我怎样才能恢复到原始值?非常感谢!为了不把这篇文章弄得乱七八糟,你可以参考这篇文章开始一个新的问题。但一个简单的答案是,你已经在每个哈希表中有了数据。匹配是关键,替换是价值。嗨,道格,正如我建议的,我打开了一个新问题:。非常感谢您的反馈。Mathias要求提供一些信息。你能提供给他吗?