Powershell - 模式匹配
我有一个要求,其中日志文件有一些IP地址,它需要被引用数据库的主机名替换。我得到的输出与在数据库中找到的主机名匹配。我无法打印没有找到主机名的IP地址,因为它可以帮助获得完整的输出。Powershell - 模式匹配
IP_Address.txt
dhhdhja sasa 10.1.154.6
sasas swssss 10.1.154.10
assas 10.1.154.14
10.1.154.34
10.1.154.38
Hostname.txt
10.1.154.6=>Host1
10.1.154.10=>Host2
10.1.154.14=>Host3
电流输出
dhhdhja sasa 10.1.154.6=>Host1
sasas swssss 10.1.154.10=>Host2
assas 10.1.154.14=>Host3
期望输出
dhhdhja sasa 10.1.154.6=>Host1
sasas swssss 10.1.154.10=>Host2
assas 10.1.154.14=>Host3
10.1.154.34
10.1.154.38
代码
$log = "C:\Users\IP_Address.txt"
[email protected]()
$DB = Get-Content C:\Users\Hostname.txt
Get-Content $log |
Where-Object {$_ -match '(?<IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'} |
ForEach-Object {
# Try to resolve the IP
Try
{
$IP = $Matches.IP
foreach($DBE in $DB)
{
if($IP -match $DBE.split("=>")[0])
{
$hostname = $DBE
if ($hostname -ne "")
{
Get-Content $log |
Where-Object {$_ -match $IP} |
ForEach-Object {
$_ -replace $IP, $hostname
}
}
}
}
}
catch
{
#$_ -replace $IP, $IP
}
}
试试这个:
$log = "C:\Users\IP_Address.txt"
[email protected]()
$DB = Get-Content C:\Users\Hostname.txt
## Loop through each line in IP_Address.txt to do the replacement, as needed
Get-Content $log | ForEach-Object {
## Perform work if we run into a line with an IP
if ($_ -match '(?<IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})') {
$DBE = $DB | ? { $_ -like "$($_)*" }
## Make sure the entry we get back is a valid IP
if ($DBE -match '(?<IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})') {
$_.Replace($_, $DBE) ## Replace just the IP with the IP/hostname from hostname.txt
} else {
$_ ## Else just emit the line as-is which still contains the original IP
}
} else {
$_
}
}
我无法获得预期的输出。以下是运行代码后的输出,它具有重复的输出,并且还排除了输出中打印的其他两个未解析的IP。 – Vijay
10.1.154.6 =>主机1 10.1.154.10 =>主机2 10.1.154.14 =>主机3 10.1.154.6 =>主机1 10.1.154.10 =>主机2 10.1.154.14 =>主机3 10.1.154.6 =>主机1 10.1.154.10 => Host2 10.1.154.14 => Host3 10.1.154.6 => Host1 10.1.154.10 => Host2 10.1.154.14 => Host3 10.1.154.6 => Host1 10.1.154.10 => Host2 10.1.154.14 => Host3 – Vijay
Hello Team,I希望有人能解决这个问题。我试过一切都没有为我工作。 – Vijay
最后我能解决这个问题.. 代码:
$log = "C:\Users\IP_Address.txt"
[email protected]()
$DB = Get-Content "C:\Users\Hostname.txt"
$file = Get-Content "C:\Users\Hostname.txt"
Get-Content $log |
Where-Object {$_ -match '(?<IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'} |
ForEach-Object {
$IP = $Matches.IP
$containsWord = $file | %{$_ -match $IP}
If($containsWord -contains $true)
{
# Try to resolve the IP
$IP = $Matches.IP
foreach($DBE in $DB)
{
if($IP -match$DBE.split("=>")[0])
{
$hostname = $DBE
if ($hostname -ne "")
{
Get-Content $log |
Where-Object {$_ -match $IP} |
ForEach-Object {
$_ -replace $IP, $hostname
}
}
}
}
}
Else{$_}
}
@TessellatingHeckler我不知道,如果是下面的逻辑我应该尝试和它仍然打印只解决IP's.Below是代码。 – Vijay
'$ log =“C:\ Users \ IP_Address.txt” $ DB = @() $ DB = Get-Content C:\ Users \ Hostname.txt Get-Content $ log | Where-Object {$ _ -match'(? \ d {1,3} \。\ d {1,3} \。\ d {1,3} \。\ d {1,3})'} | 的ForEach-对象{ 的foreach(在$ $ DB DBE) { 如果($ _ -match $ DBE.Split( “=>”)[0]) { 获取内容$ log | 的foreach对象{ 如果($ _ $ -match IP){$ _ $ -replace IP,$ DBE} }} } }' –
Vijay
你好队,我将不胜感激,如果任何人可以解决这个问题。我试过一切都没有为我工作。 – Vijay