首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我需要在每一行中增加变量,但也需要在一行结束后重新设置计数器。

我需要在每一行中增加变量,但也需要在一行结束后重新设置计数器。
EN

Stack Overflow用户
提问于 2017-05-23 11:18:09
回答 2查看 253关注 0票数 1

我是Powershell的新手,所以在这里寻求帮助。

我需要一个PowerShell脚本来读取txt文件并增加每个x变量+1,但是计数器会在1行后重置。

样本(原件):

推广活动于2017年5月8日晚12:00开始, id=" 8“/>and将于2017年6月9日晚11:59结束(” id=" 9“/>Promotion June id="10”/>“)

期望产出:

id=" 8“/>and于2017年6月9日晚11:59结束(” id=" 9“/>Promotion June id="10”/>“)

这是一行,所以计数器应该被重新设置为1(1.n)

我找到了一个脚本,它获取所有的文件内容,并增加所有的值,这不是我想要的。

我仍在研究,所以稍后会更新这个。

到目前为止更新脚本:

代码语言:javascript
复制
$reader = [System.IO.File]::OpenText("testfile")
#IF ([System.IO.File]::Exists($args[0])) { $contents = [System.IO.File]::ReadAllText($args[0]) } ELSE {  
   # ECHO "File does not exist!" }
try {
    for() {
        $line = $reader.ReadLine()
        if ($line -eq $null) { break }
          
        $numbers =  
        [System.Text.RegularExpressions.Regex]::Matches($line, "<x")

        for ($i = $numbers.Count - 1; $i -ge 0; $i--)
{
    
    Write-Host ($numbers[$i].Index + $numbers[$i].Length)
    
    $line = $line.Substring(0, $numbers[$i].Index) +  
        ($i + 1).ToString() +  
        $line.Substring($numbers[$i].Index + $numbers[$i].Length)
}

        $line
    }
}
finally {
    $reader.Close()
}

它实现了增量,但是实际的regex匹配在代码之后不匹配:

促销活动于2017年5月8日晚12:00开始,1 id=" 8“/>and将于2017年6月9日晚11:59结束(”2 id="“/>Promotion June 3 id= "10”/>“)。

它应该是:

促销活动于2017年5月8日晚12:00开始, id=" 8“/>and ends >于2017年6月9日晚11:59开始(”id=“/>Promotion June > id="10”/>“)。

提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-23 12:41:01

您发布的脚本正在更改,但缺少了

代码语言:javascript
复制
$reader = [System.IO.File]::OpenText("testfile")
#IF ([System.IO.File]::Exists($args[0])) { $contents = [System.IO.File]::ReadAllText($args[0]) } ELSE {  
   # ECHO "File does not exist!" }
try {
    for() {
        $line = $reader.ReadLine()
        if ($line -eq $null) { break }

        $numbers =  [System.Text.RegularExpressions.Regex]::Matches($line, "\<x")

        for ($i = $numbers.Count - 1; $i -ge 0; $i--)
        {

            Write-Host ($numbers[$i].Index + $numbers[$i].Length)
            $line = $line.Substring(0, $numbers[$i].Index) +  "<x" + ($i + 1).ToString() +  $line.Substring($numbers[$i].Index + $numbers[$i].Length)
        }

        $line
    }
}
finally {
    $reader.Close()
}
票数 1
EN

Stack Overflow用户

发布于 2017-05-23 11:39:04

您有到目前为止尝试过的脚本吗?还是希望社区为您编写它?

不管是哪种方式,这都是社区其他成员调试和完善.

代码语言:javascript
复制
$content = 'Promotion begins at 12:00 am ET on May 8, 2017 <x3 id="8" />and ends at 11:59 pm ET on June 9, 2017 (“<x id="9" />Promotion Period <x id="10" />”)'
$test = ""

ForEach($line in $content){
$splitline = $line -match '\<x\d* id\='
switch ($Matches[0]){
     "<x id=" {$test = $line.replace($Matches[0],"<x1 id=") ; break}
     "<x1 id=" {$test = $line.replace($Matches[0],"<x2 id=") ; break}
     "<x2 id=" {$test = $line.replace($Matches[0],"<x3 id=") ; break}
     "<x3 id=" {$test = $line.replace($Matches[0],"<x4 id=") ; break}
     "<x4 id=" {$test = $line.replace($Matches[0],"<x5 id=") ; break}
     default {"Something else happened"; break}
     }
write-host $test
    }

对不起,我不会继续更新上面的片段,因为您对原来的帖子做了更改。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44133293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档