Powershell 如果匹配模式后接下一个模式与power shell中的计数编号不匹配,则需要添加4行
您好,我的输入文件如下所示,我的要求是添加4行,如果macthing模式后面的下一个模式与计数编号一起未匹配 我将检查查找套接字,如果匹配项将使行数增加到+1,我将获得下一行并查找单词“address”,如果地址不存在,我需要在该行插入一组行“communication.manageraddress_9,communication.manageraddress_10,communication.manageraddress_11”netx communication.manageradress_7=xxx.comPowershell 如果匹配模式后接下一个模式与power shell中的计数编号不匹配,则需要添加4行,powershell,text,count,Powershell,Text,Count,您好,我的输入文件如下所示,我的要求是添加4行,如果macthing模式后面的下一个模式与计数编号一起未匹配 我将检查查找套接字,如果匹配项将使行数增加到+1,我将获得下一行并查找单词“address”,如果地址不存在,我需要在该行插入一组行“communication.manageraddress_9,communication.manageraddress_10,communication.manageraddress_11”netx communication.manageradress_
communication.managerid_7=xxx
communication.managerport_7=xxx
通讯插座7=xx
communication.manageradress_8=xxx.com
communication.managerid_8=xxx
communication.managerport_8=xxx
通信插座8=普通的
由经理添加 communication.managerhealthmon_4=true
通信协议REV_4=3
communication.managerhealthmon_1=true
通信协议1=2
输出将是这样的 communication.manageradress_7=xxx.com
communication.managerid_7=xxx
communication.managerport_7=xxx
通讯插座7=xx
communication.manageradress_8=xxx.com
communication.managerid_8=xxx
communication.managerport_8=xxx
通信插座8=普通的
communication.manageradress_9=xxx.com
communication.managerid_9=xxx
communication.managerport_9=xxx
通信插座9=普通的
communication.manageradress_10=xxx.com
communication.managerid_10=xxx
communication.managerport\u 1o=xxx
通信.套接字\u 1o=普通
由经理添加 communication.managerhealthmon_4=true
通信协议REV_4=3
communication.managerhealthmon_1=true
通信协议1=2
这是我的脚本,我被插入到文本文件和增量数字击中,可以在powershell中有所帮助
$files = $File = 'C:\Users\rseerala\Desktop\ARUN\in.txt'
#$NewContent = Get-Content -Path $File
foreach($file in $files){
$content = Get-Content $file
for($i = 0; $i -lt $content.Count; $i++){
$line = $content[$i]
if ($line.Contains("socket"))
{
$line = $content[$i+2]
if ($line.Contains("address"))
{
Write-Host "This line starts with 6"
}}}}
好的,如果我理解正确,这就是你想要的:
#read the file as a single multiline string
$txt = Get-Content -Path 'C:\Users\rseerala\Desktop\ARUN\in.txt' -Raw
# if it contains the magic word '.socket_' followed by a number
if ($txt -match '\.socket_\d+') {
# first split off the 'Added by Manager' stuff
$content, $managerAdded = ($txt -split 'Added by Manager').Trim()
# split the content part into separate blocks of 4 lines
$blocks = $content -split '(\r?\n){2}' | Where-Object { $_ -match '\S' }
# get the index value from the last block
$index = [int]([regex] '(?i)\.socket_(\d+)').Match($blocks[-1]).Groups[1].Value
# now repeat the blocks you already have and output copies with incremented indices
$newBlocks = ($blocks | ForEach-Object {
$_ -replace '_\d+=', ('_{0}=' -f ++$index)
}) -join "`r`n`r`n"
# finally, combine the content part with the new blocks
# and the 'Added by Manager' lines with double newlines
$result = $content, $newBlocks, 'Added by Manager', $managerAdded -join "`r`n`r`n"
# output on screen
$result
# write to a new file
$result | Set-Content -Path 'C:\Users\rseerala\Desktop\ARUN\out.txt'
}
else {
Write-Warning "The file does not contain the word '.socket_' followed by a number.."
}
输出:
communication.manageraddress_7=xxx.com
communication.managerid_7=xxx
communication.managerport_7=xxx
communication.socket_7=xx
communication.manageraddress_8=xxx.com
communication.managerid_8=xxx
communication.managerport_8=xxx
communication.socket_8=plain
communication.manageraddress_9=xxx.com
communication.managerid_9=xxx
communication.managerport_9=xxx
communication.socket_9=plain
communication.manageraddress_10=xxx.com
communication.managerid_10=xxx
communication.managerport_10=xxx
communication.socket_10=plain
Added by Manager
communication.managerhealthmon_4=true
communication.protocolrev_4=3
communication.managerhealthmon_1=true
communication.protocolrev_1=2
communication.manageradress_7=xxx.com
通信。managerid_7=xxx
通信管理器端口7=xxx
通讯插座7=xx
communication.manageradress_8=xxx.com
通信。managerid_8=xxx
通信.managerport_8=xxx
通信插座8=普通
communication.manageradress_9=xxx.com
通信。managerid_9=xxx
通信管理器端口9=xxx
通信插座9=普通
communication.manageradress_10=xxx.com
通信。managerid_10=xxx
通信管理器端口10=xxx
通信插座10=普通
由经理添加
communication.managerhealthmon_4=true
通信协议REV_4=3
communication.managerhealthmon_1=true
通信协议REV_1=2
好的,如果我理解正确,这就是你想要的:
#read the file as a single multiline string
$txt = Get-Content -Path 'C:\Users\rseerala\Desktop\ARUN\in.txt' -Raw
# if it contains the magic word '.socket_' followed by a number
if ($txt -match '\.socket_\d+') {
# first split off the 'Added by Manager' stuff
$content, $managerAdded = ($txt -split 'Added by Manager').Trim()
# split the content part into separate blocks of 4 lines
$blocks = $content -split '(\r?\n){2}' | Where-Object { $_ -match '\S' }
# get the index value from the last block
$index = [int]([regex] '(?i)\.socket_(\d+)').Match($blocks[-1]).Groups[1].Value
# now repeat the blocks you already have and output copies with incremented indices
$newBlocks = ($blocks | ForEach-Object {
$_ -replace '_\d+=', ('_{0}=' -f ++$index)
}) -join "`r`n`r`n"
# finally, combine the content part with the new blocks
# and the 'Added by Manager' lines with double newlines
$result = $content, $newBlocks, 'Added by Manager', $managerAdded -join "`r`n`r`n"
# output on screen
$result
# write to a new file
$result | Set-Content -Path 'C:\Users\rseerala\Desktop\ARUN\out.txt'
}
else {
Write-Warning "The file does not contain the word '.socket_' followed by a number.."
}
输出:
communication.manageraddress_7=xxx.com
communication.managerid_7=xxx
communication.managerport_7=xxx
communication.socket_7=xx
communication.manageraddress_8=xxx.com
communication.managerid_8=xxx
communication.managerport_8=xxx
communication.socket_8=plain
communication.manageraddress_9=xxx.com
communication.managerid_9=xxx
communication.managerport_9=xxx
communication.socket_9=plain
communication.manageraddress_10=xxx.com
communication.managerid_10=xxx
communication.managerport_10=xxx
communication.socket_10=plain
Added by Manager
communication.managerhealthmon_4=true
communication.protocolrev_4=3
communication.managerhealthmon_1=true
communication.protocolrev_1=2
communication.manageradress_7=xxx.com
通信。managerid_7=xxx
通信管理器端口7=xxx
通讯插座7=xx
communication.manageradress_8=xxx.com
通信。managerid_8=xxx
通信.managerport_8=xxx
通信插座8=普通
communication.manageradress_9=xxx.com
通信。managerid_9=xxx
通信管理器端口9=xxx
通信插座9=普通
communication.manageradress_10=xxx.com
通信。managerid_10=xxx
通信管理器端口10=xxx
通信插座10=普通
由经理添加
communication.managerhealthmon_4=true
通信协议REV_4=3
communication.managerhealthmon_1=true
通信协议REV_1=2
真的我不知道你在这里想干什么。重复(4行)文本块,并在找到单词
套接字后立即增加=
符号前的数字?是Theo..正确..诚实。。我不知道你在这里想干什么。重复(4行)文本块,并在找到单词套接字后立即增加=
符号前的数字?是Theo..correct。。