Html 我需要在每行中增加变量,但也需要在行结束后重置计数器
我是Powershell的新手,请在此寻求帮助 我需要一个PowerShell脚本,它将读取txt文件并将每个x变量+1递增,但计数器将在1行后重置 样品(原件): 促销活动于2017年5月8日美国东部时间上午12:00开始,于2017年6月9日美国东部时间晚上11:59结束(促销期) 期望输出: 促销活动于2017年5月8日美国东部时间上午12:00开始,于2017年6月9日美国东部时间晚上11:59结束(促销期) 这是一行,因此在它之后计数器应再次重置为1(1..n) 我发现了一个脚本,它获取所有文件内容并增加所有值,这不是我想要的 我仍在研究,所以稍后将更新此 到目前为止更新脚本:Html 我需要在每行中增加变量,但也需要在行结束后重置计数器,html,powershell,replace,Html,Powershell,Replace,我是Powershell的新手,请在此寻求帮助 我需要一个PowerShell脚本,它将读取txt文件并将每个x变量+1递增,但计数器将在1行后重置 样品(原件): 促销活动于2017年5月8日美国东部时间上午12:00开始,于2017年6月9日美国东部时间晚上11:59结束(促销期) 期望输出: 促销活动于2017年5月8日美国东部时间上午12:00开始,于2017年6月9日美国东部时间晚上11:59结束(促销期) 这是一行,因此在它之后计数器应再次重置为1(1..n) 我发现了一个脚本,它获
$reader=[System.IO.File]::OpenText(“测试文件”)
#如果([System.IO.File]::存在($args[0]){$contents=[System.IO.File]::ReadAllText($args[0])}其他{
#ECHO“文件不存在!”}
试一试{
for(){
$line=$reader.ReadLine()
如果($line-eq$null){break}
$numbers=
[System.Text.RegularExpressions.Regex]::匹配($line,“x”)
对于($i=$numbers.Count-1;$i-ge 0;$i--)
{
写入主机($numbers[$i]。索引+$numbers[$i]。长度)
$line=$line.Substring(0,$numbers[$i].Index)+
($i+1).ToString()+
$line.Substring($numbers[$i].Index+$numbers[$i].Length)
}
美元线
}
}
最后{
$reader.Close()
}
到目前为止,您是否有尝试过的脚本,或者您希望社区为您编写脚本
不管怎样,社区的其他成员都可以从这里开始调试和完善
$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
}
$content='促销活动于2017年5月8日东部时间上午12:00开始,于2017年6月9日东部时间下午11:59结束(“促销期”)'
$test=“”
ForEach($content中的行){
$splitline=$line-match'\您发布的脚本正在更改,但缺少
$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()
}
$reader=[System.IO.File]::OpenText(“测试文件”)
#如果([System.IO.File]::存在($args[0]){$contents=[System.IO.File]::ReadAllText($args[0])}其他{
#ECHO“文件不存在!”}
试一试{
for(){
$line=$reader.ReadLine()
如果($line-eq$null){break}
$numbers=[System.Text.RegularExpressions.Regex]::匹配($line,"\您尝试了什么,以及您尝试了什么失败了?理想情况下,您应该提供您尝试过的内容的详细信息,并包括关于失败原因的具体信息,以及错误消息和/或错误输出。因此,不是代码编写服务;最好的问题是那些提供有用信息的问题,以便回答者能够指导您进行代码编写查看您自己的正确答案。请看。@JeffZeitlin说得好,您比我快。Petras K-请发布一个尝试,以便我们能够提供帮助,因为解决方案并不困难。据我所知,最后一个问题是,我需要重写$reader部分,以使内容能够实际将控制台输出保存到文件,还是有办法将$reader传输到文件?再次感谢您在“不想使用管道读取器”上,您想将$line写入新文件。PowerShell中有大量资源可用于写入文件。请尝试一下,如果需要帮助,请发布您遇到的问题。