捕获Process算法的输出并替换初始的$ data字符串

时间:2017-02-17 07:25:13

标签: arrays string powershell parsing output

我有一个忽略某些文本块的脚本,但我不知道如何将被忽略的文本替换为原始字符串,以便将其删除。

我尝试创建一个数组向量,但是powershell通过赋值做了奇怪的事情,例如$ array + = $ _附加了比它应该更多的文本...

任何帮助表示感谢,这是我的脚本:

#$path = (Get-Item -Path ".\" -Verbose).FullName
$path = split-path -parent $MyInvocation.MyCommand.Definition
$files = Get-ChildItem "$path\test" -r # root path $PSScriptRoot

#echo $path
#echo $files
#echo $files.Count
ForEach ($file in $files){

    #echo "the value of i is" $i
    #echo $file.FullName
    #iterate through files from the current folder.
    $data = Get-Content -Path $files.FullName

    #echo "$data"

    # parse DisabledFeatures.txt file as array of strings (1 string per line of the file)
    $feature = Get-Content "$path\Disabled_Features.txt"
    echo $feature.Count   

    #[System.Collections.ArrayList]$Modifier
    $nl=[Environment]::NewLine
    #iterate for each string entry in $feature array (read from txt file)
    for($counter=0; $counter -lt $feature.Count; $counter++){
        #Start ignoring text after we've found the trigger
        $parse = $feature[$counter]
        #$Modifier.Clear()
        $data | ForEach-Object -Begin { 
            $ignore = $false; $levels = 0 
        } -Process {

            if($_ -match "^#ifdef $parse") { 
                $ignore = $true 
                #echo "start ignore"

            }if($ignore) { #Track nested groups
                if ($_ -match "^#ifdef") {  
                    $levels++ 
                    #echo "levels++"
                }elseif ($_ -match "#endif") {
                    if($levels -ge 1) { 
                        $levels--
                        #echo "levels --" 
                    }else { #If no nesting, we've hit the end of our targeted group. Stop ignoring
                        $ignore = $false 
                        #echo "end ignore"
                    }
                }
            }else {  #Append line
                $temp=$_
                $Modifier+="$temp$nl"
            }
        }
        echo $Modifier 
    }
}

1 个答案:

答案 0 :(得分:0)

我已经通过编写$ data = $ data |找到了解决方案ForEach-Object -Begin {.......然后将输出重定向为:$ data |输出文件"路径"

相关问题