检查 csv 是否有空白字段,如果存在空白则写入输出
check csv for blank fields and write output if exist blank
这是一个 csv 示例:
1- 2018-11-07,hostname-184,IP_INFO, 10.2334.40.334, 255.255.255.0,
2 - 2018-11-07,hostname-184,IP_INFO, 334.204.334.68, 255.255.255.0,
3- 2018-11-07,hostname,7.1.79-8,IP_INFO, 142.334.89.3342, 255.255.255.0,
4- 2018-11-07,hostname,7.1.80-7,IP_INFO, 13342.221.334.87, 255.255.255.0,
5- 2018-11-07,hostname-155,IP_INFO, 142.2334.92.212, 255.255.255.0,
6 - 2018-11-07,hostname-184,IP_INFO, , , 1
7- 2018-11-07,hostname-184,IP_INFO, 10.19334.60.3343, 255.255.255.0,
那么我如何检查最后两个 space 是否为空白(如第 6 行)?
想法是使用这样的东西:
$contentdnsparsed = Get-Content $destination_RAW_NAS\DNS_NAS_PARSED_0
For($i=0;$i -lt $contentdnsparsed.count;$i++){
if($contentdnsparsed[$i] -match "running")
{
$Global:MatchDNS = $OK } Else {$Global:MatchDNS = $FAIL }
}
If match "something" in the space 4 and 5 after the "," output = OK else = FAIL.
谢谢大家
尽管您为我们提供了一个 CSV 文件的相当糟糕的示例,但您应该使用 Import-Csv
cmdlet。
因为 csv 没有 headers,您需要使用 -Header
参数提供它们,如下所示:
$csvContent = Import-Csv -Path "$destination_RAW_NAS\DNS_NAS_PARSED_0" -Header @("Date","HostName", "InfoType","IPAddress","Subnet")
$csvContent | ForEach-Object {
# test for empty IPAddress fields in the CSV
if ([string]::IsNullOrEmpty($_.IPAddress)) {
Write-Host "$($_.HostName) = FAIL" -ForegroundColor Red
# somewhere in your code you have declared the variables $Global:MatchDNS, $FAIL and $OK I guess..
$Global:MatchDNS = $FAIL
}
else {
Write-Host "$($_.HostName) = OK" -ForegroundColor Green
$Global:MatchDNS = $OK
}
}
希望对您有所帮助
这是一个 csv 示例:
1- 2018-11-07,hostname-184,IP_INFO, 10.2334.40.334, 255.255.255.0,
2 - 2018-11-07,hostname-184,IP_INFO, 334.204.334.68, 255.255.255.0,
3- 2018-11-07,hostname,7.1.79-8,IP_INFO, 142.334.89.3342, 255.255.255.0,
4- 2018-11-07,hostname,7.1.80-7,IP_INFO, 13342.221.334.87, 255.255.255.0,
5- 2018-11-07,hostname-155,IP_INFO, 142.2334.92.212, 255.255.255.0,
6 - 2018-11-07,hostname-184,IP_INFO, , , 1
7- 2018-11-07,hostname-184,IP_INFO, 10.19334.60.3343, 255.255.255.0,
那么我如何检查最后两个 space 是否为空白(如第 6 行)?
想法是使用这样的东西:
$contentdnsparsed = Get-Content $destination_RAW_NAS\DNS_NAS_PARSED_0
For($i=0;$i -lt $contentdnsparsed.count;$i++){
if($contentdnsparsed[$i] -match "running")
{
$Global:MatchDNS = $OK } Else {$Global:MatchDNS = $FAIL }
}
If match "something" in the space 4 and 5 after the "," output = OK else = FAIL.
谢谢大家
尽管您为我们提供了一个 CSV 文件的相当糟糕的示例,但您应该使用 Import-Csv
cmdlet。
因为 csv 没有 headers,您需要使用 -Header
参数提供它们,如下所示:
$csvContent = Import-Csv -Path "$destination_RAW_NAS\DNS_NAS_PARSED_0" -Header @("Date","HostName", "InfoType","IPAddress","Subnet")
$csvContent | ForEach-Object {
# test for empty IPAddress fields in the CSV
if ([string]::IsNullOrEmpty($_.IPAddress)) {
Write-Host "$($_.HostName) = FAIL" -ForegroundColor Red
# somewhere in your code you have declared the variables $Global:MatchDNS, $FAIL and $OK I guess..
$Global:MatchDNS = $FAIL
}
else {
Write-Host "$($_.HostName) = OK" -ForegroundColor Green
$Global:MatchDNS = $OK
}
}
希望对您有所帮助