Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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将字符串中的第26个字符替换为序列号_Powershell_Replace_Character_Substring - Fatal编程技术网

Powershell将字符串中的第26个字符替换为序列号

Powershell将字符串中的第26个字符替换为序列号,powershell,replace,character,substring,Powershell,Replace,Character,Substring,我想替换以“M”开头的字符串的第26个字符,并且我想在以后的任何字符串中放置一个从零开始的序列号和序列#+1 文件包含以下字符串: M453023455697RR23047230426485111814xxxxx 拉帕雷尔/配件 鞋 L0398479823940970924MM0970974534097xxxxxx M98802734207KK9728394823264858111814xxxxx 我的Powershell脚本: $SequenceStart=1 $String=“M” $s

  • 我想替换以“M”开头的字符串的第26个字符,并且我想在以后的任何字符串中放置一个从零开始的序列号和序列#+1

  • 文件包含以下字符串:

  • M453023455697RR23047230426485111814xxxxx
    拉帕雷尔/配件
    鞋
    L0398479823940970924MM0970974534097xxxxxx
    M98802734207KK9728394823264858111814xxxxx
    

  • 我的Powershell脚本:
  • $SequenceStart=1
    $String=“M”
    $str2=“$xfiles.substring(0,24)”
    $str3=“$xfiles.substring(25)”
    $str4=“$xfiles.substring(26,53)”
    $str5=“$xfiles.substring(0,80)”
    $Line=“$str2”“$str+1”“$str4”
    $xfiles=获取子项$infles
    写入主机“找到”($xfiles |度量对象)。计算“要处理的文件”
    如果($xfiles |度量对象).count-gt 0){
    foreach($X文件中的X文件)
    {
    写入主机“处理文件:$xFile`r”
    $cnt=$SequenceStart
    $content=获取内容-路径$xfile
    $content | foreach{
    如果($\包含($String)){
    $\替换$String,$Line}
    else{$}
    
    工作无聊……好吧,我知道最简单的方法是:

    用Get Content读取文件,遍历所有行,如果以M开头,则替换并递增计数器

    foreach ($xfile in $xfiles)
    {
        Write-Host "Processing file: $xFile`r"
        $counter = 0
        (Get-Content $xfile | ForEach{
            If($_ -match "^m"){$_ -replace "(?<=^.{25})(.)",$counter;$counter++}else{$_}
        })|out-file $xfile.fullname
    }
    
    foreach($xfile中的xfile)
    {
    写入主机“处理文件:$xFile`r”
    $counter=0
    (获取内容$xfile | ForEach{
    如果($\匹配“^m”){$\替换”(?另一种可能性:

    foreach ($xfile in $xfiles)
    {
        Write-Host "Processing file: $xFile`r"
        $counter = 0
        (Get-Content $xfile | ForEach{
          if ($_ -match '^(M.{24}).(.+)')
            {'{0}{1}{2}' -f $matches[1],$counter++,$matches[2]}
          else {$_}  
        })|out-file $xfile.fullname
    }
    

    还有另外一个只适合恶心的选择:

    foreach ($xFile in $xFiles) {
        Write-Progress -Activity "Processing file: $xFile"
        $counter = 0
        Get-Content $xFile | Foreach {
            [regex]::replace($_, '(^M.{24}).(.*)', 
                {param($m) $m.Groups[1].Value + ($global:counter++) + $m.Groups[2].Value})
        } | Out-File $xFile.FullName
    }
    

    必须喜欢好的ol'MatchEvaluator,但不幸的是,由于PowerShell处理回调的方式,这种方法很慢。

    您有问题吗?您的代码也不完整,因为我们看不到在哪里声明了
    $infles
    。这并不是说它真的影响了问题,而是可能缺少了其他东西,如右括号在
    if
    语句和
    foreach
    上。既然你没有说,到底是什么错了?是的,这个脚本不起作用,我想知道如何实现。我还忘了声明一个变量$str=$str3。但即使在这之后它也不起作用。谢谢$InFiles=CDMS.txt的路径,它是包含数据的文件。我想知道如何编写IF语句或是否可以调整此脚本。任何提示都将不胜感激。序列号…这是否表示M行的每个匹配项?因此文件中的第5个M行的序列号为5?它是否替换第26个字符或将其向下推。一旦完成,您将如何处理数据?将它写回同一个文件?做得好。好久没见到你了。Regex将成为搜索引擎。我也没想到用它来替换。我在微软得到了一份新的工作,作为一名供应商,所以我一直在忙着学习诀窍。不过,应该会定期回来=)嗯,你先做
    $Counter=0
    ,然后再做
    $i++
    。我不记得我可以同时递增和使用变量。我想我可以把我的
    $Counter;$Counter++
    缩短为
    $Counter++
    。忽略了这一点。谢谢你的提示。编写代码需要更多的工作,但可能更直观,这取决于你r regex的舒适度。谢谢大家。所有的解决方案都有效,但我使用了Keith建议的解决方案。谢谢Keith。