check csv for blank fields and write output if exist blank









up vote
0
down vote

favorite












This is a csv example:



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,


so how can i check if the las two spaces are in blank (like line 6 ) ?



The idea is to use something like this:



 $contentdnsparsed = Get-Content $destination_RAW_NASDNS_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.



Thank you guys










share|improve this question





















  • Your whole sample csv has no text running - so how could that work? I'd use Import-Csv and supply header names.
    – LotPings
    Nov 9 at 19:23










  • if the lines are consistent with the pattern you show, then you can test for -match ', , ,' to id the line with missing data. that will match [or -notmatch] on three commas delimited by a single space.
    – Lee_Dailey
    Nov 9 at 19:32














up vote
0
down vote

favorite












This is a csv example:



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,


so how can i check if the las two spaces are in blank (like line 6 ) ?



The idea is to use something like this:



 $contentdnsparsed = Get-Content $destination_RAW_NASDNS_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.



Thank you guys










share|improve this question





















  • Your whole sample csv has no text running - so how could that work? I'd use Import-Csv and supply header names.
    – LotPings
    Nov 9 at 19:23










  • if the lines are consistent with the pattern you show, then you can test for -match ', , ,' to id the line with missing data. that will match [or -notmatch] on three commas delimited by a single space.
    – Lee_Dailey
    Nov 9 at 19:32












up vote
0
down vote

favorite









up vote
0
down vote

favorite











This is a csv example:



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,


so how can i check if the las two spaces are in blank (like line 6 ) ?



The idea is to use something like this:



 $contentdnsparsed = Get-Content $destination_RAW_NASDNS_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.



Thank you guys










share|improve this question













This is a csv example:



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,


so how can i check if the las two spaces are in blank (like line 6 ) ?



The idea is to use something like this:



 $contentdnsparsed = Get-Content $destination_RAW_NASDNS_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.



Thank you guys







powershell csv parsing powershell-v3.0 text-parsing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 19:02









Agustin lacorazza

426




426











  • Your whole sample csv has no text running - so how could that work? I'd use Import-Csv and supply header names.
    – LotPings
    Nov 9 at 19:23










  • if the lines are consistent with the pattern you show, then you can test for -match ', , ,' to id the line with missing data. that will match [or -notmatch] on three commas delimited by a single space.
    – Lee_Dailey
    Nov 9 at 19:32
















  • Your whole sample csv has no text running - so how could that work? I'd use Import-Csv and supply header names.
    – LotPings
    Nov 9 at 19:23










  • if the lines are consistent with the pattern you show, then you can test for -match ', , ,' to id the line with missing data. that will match [or -notmatch] on three commas delimited by a single space.
    – Lee_Dailey
    Nov 9 at 19:32















Your whole sample csv has no text running - so how could that work? I'd use Import-Csv and supply header names.
– LotPings
Nov 9 at 19:23




Your whole sample csv has no text running - so how could that work? I'd use Import-Csv and supply header names.
– LotPings
Nov 9 at 19:23












if the lines are consistent with the pattern you show, then you can test for -match ', , ,' to id the line with missing data. that will match [or -notmatch] on three commas delimited by a single space.
– Lee_Dailey
Nov 9 at 19:32




if the lines are consistent with the pattern you show, then you can test for -match ', , ,' to id the line with missing data. that will match [or -notmatch] on three commas delimited by a single space.
– Lee_Dailey
Nov 9 at 19:32












1 Answer
1






active

oldest

votes

















up vote
5
down vote



accepted










Although you give us a rather bad example of a CSV file, you should use the Import-Csv cmdlet.
Because the csv has no headers, you need to supply these with the -Header parameter like below:



$csvContent = Import-Csv -Path "$destination_RAW_NASDNS_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




Hope that helps






share|improve this answer




















  • [string]::IsNullOrWhiteSpace() may work better here than [string]::IsNullOrEmpty().
    – Bacon Bits
    Nov 9 at 19:50










  • @BaconBits I disagree that [string]::IsNullOrWhiteSpace() would be better here, because the whitespace in the csv is not enclosed in quote characters (as if it IS the fields value), and therefore it is 'eaten' by the Import-Csv cmdlet. It's effect wil be the same here.
    – Theo
    Nov 9 at 20:04







  • 1




    Yeah, but if the file is ever field quoted because something changed upstream, the script will unexpectedly work differently. It's not like it's a well-documented, guaranteed behavior of Import-Csv; it's just an implementation detail. It's not like String.IsNullOrWhiteSpace() should be avoided for performance or compatibility reasons, either. The bottom line is, "Do I ever want to treat a string that contains nothing but whitespace as though it were a valid IP address?" and that answer is going to be, "No, that's not ever going to be a valid IP address." So code for it.
    – Bacon Bits
    Nov 9 at 20:50










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231838%2fcheck-csv-for-blank-fields-and-write-output-if-exist-blank%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
5
down vote



accepted










Although you give us a rather bad example of a CSV file, you should use the Import-Csv cmdlet.
Because the csv has no headers, you need to supply these with the -Header parameter like below:



$csvContent = Import-Csv -Path "$destination_RAW_NASDNS_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




Hope that helps






share|improve this answer




















  • [string]::IsNullOrWhiteSpace() may work better here than [string]::IsNullOrEmpty().
    – Bacon Bits
    Nov 9 at 19:50










  • @BaconBits I disagree that [string]::IsNullOrWhiteSpace() would be better here, because the whitespace in the csv is not enclosed in quote characters (as if it IS the fields value), and therefore it is 'eaten' by the Import-Csv cmdlet. It's effect wil be the same here.
    – Theo
    Nov 9 at 20:04







  • 1




    Yeah, but if the file is ever field quoted because something changed upstream, the script will unexpectedly work differently. It's not like it's a well-documented, guaranteed behavior of Import-Csv; it's just an implementation detail. It's not like String.IsNullOrWhiteSpace() should be avoided for performance or compatibility reasons, either. The bottom line is, "Do I ever want to treat a string that contains nothing but whitespace as though it were a valid IP address?" and that answer is going to be, "No, that's not ever going to be a valid IP address." So code for it.
    – Bacon Bits
    Nov 9 at 20:50














up vote
5
down vote



accepted










Although you give us a rather bad example of a CSV file, you should use the Import-Csv cmdlet.
Because the csv has no headers, you need to supply these with the -Header parameter like below:



$csvContent = Import-Csv -Path "$destination_RAW_NASDNS_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




Hope that helps






share|improve this answer




















  • [string]::IsNullOrWhiteSpace() may work better here than [string]::IsNullOrEmpty().
    – Bacon Bits
    Nov 9 at 19:50










  • @BaconBits I disagree that [string]::IsNullOrWhiteSpace() would be better here, because the whitespace in the csv is not enclosed in quote characters (as if it IS the fields value), and therefore it is 'eaten' by the Import-Csv cmdlet. It's effect wil be the same here.
    – Theo
    Nov 9 at 20:04







  • 1




    Yeah, but if the file is ever field quoted because something changed upstream, the script will unexpectedly work differently. It's not like it's a well-documented, guaranteed behavior of Import-Csv; it's just an implementation detail. It's not like String.IsNullOrWhiteSpace() should be avoided for performance or compatibility reasons, either. The bottom line is, "Do I ever want to treat a string that contains nothing but whitespace as though it were a valid IP address?" and that answer is going to be, "No, that's not ever going to be a valid IP address." So code for it.
    – Bacon Bits
    Nov 9 at 20:50












up vote
5
down vote



accepted







up vote
5
down vote



accepted






Although you give us a rather bad example of a CSV file, you should use the Import-Csv cmdlet.
Because the csv has no headers, you need to supply these with the -Header parameter like below:



$csvContent = Import-Csv -Path "$destination_RAW_NASDNS_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




Hope that helps






share|improve this answer












Although you give us a rather bad example of a CSV file, you should use the Import-Csv cmdlet.
Because the csv has no headers, you need to supply these with the -Header parameter like below:



$csvContent = Import-Csv -Path "$destination_RAW_NASDNS_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




Hope that helps







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 19:26









Theo

2,5461318




2,5461318











  • [string]::IsNullOrWhiteSpace() may work better here than [string]::IsNullOrEmpty().
    – Bacon Bits
    Nov 9 at 19:50










  • @BaconBits I disagree that [string]::IsNullOrWhiteSpace() would be better here, because the whitespace in the csv is not enclosed in quote characters (as if it IS the fields value), and therefore it is 'eaten' by the Import-Csv cmdlet. It's effect wil be the same here.
    – Theo
    Nov 9 at 20:04







  • 1




    Yeah, but if the file is ever field quoted because something changed upstream, the script will unexpectedly work differently. It's not like it's a well-documented, guaranteed behavior of Import-Csv; it's just an implementation detail. It's not like String.IsNullOrWhiteSpace() should be avoided for performance or compatibility reasons, either. The bottom line is, "Do I ever want to treat a string that contains nothing but whitespace as though it were a valid IP address?" and that answer is going to be, "No, that's not ever going to be a valid IP address." So code for it.
    – Bacon Bits
    Nov 9 at 20:50
















  • [string]::IsNullOrWhiteSpace() may work better here than [string]::IsNullOrEmpty().
    – Bacon Bits
    Nov 9 at 19:50










  • @BaconBits I disagree that [string]::IsNullOrWhiteSpace() would be better here, because the whitespace in the csv is not enclosed in quote characters (as if it IS the fields value), and therefore it is 'eaten' by the Import-Csv cmdlet. It's effect wil be the same here.
    – Theo
    Nov 9 at 20:04







  • 1




    Yeah, but if the file is ever field quoted because something changed upstream, the script will unexpectedly work differently. It's not like it's a well-documented, guaranteed behavior of Import-Csv; it's just an implementation detail. It's not like String.IsNullOrWhiteSpace() should be avoided for performance or compatibility reasons, either. The bottom line is, "Do I ever want to treat a string that contains nothing but whitespace as though it were a valid IP address?" and that answer is going to be, "No, that's not ever going to be a valid IP address." So code for it.
    – Bacon Bits
    Nov 9 at 20:50















[string]::IsNullOrWhiteSpace() may work better here than [string]::IsNullOrEmpty().
– Bacon Bits
Nov 9 at 19:50




[string]::IsNullOrWhiteSpace() may work better here than [string]::IsNullOrEmpty().
– Bacon Bits
Nov 9 at 19:50












@BaconBits I disagree that [string]::IsNullOrWhiteSpace() would be better here, because the whitespace in the csv is not enclosed in quote characters (as if it IS the fields value), and therefore it is 'eaten' by the Import-Csv cmdlet. It's effect wil be the same here.
– Theo
Nov 9 at 20:04





@BaconBits I disagree that [string]::IsNullOrWhiteSpace() would be better here, because the whitespace in the csv is not enclosed in quote characters (as if it IS the fields value), and therefore it is 'eaten' by the Import-Csv cmdlet. It's effect wil be the same here.
– Theo
Nov 9 at 20:04





1




1




Yeah, but if the file is ever field quoted because something changed upstream, the script will unexpectedly work differently. It's not like it's a well-documented, guaranteed behavior of Import-Csv; it's just an implementation detail. It's not like String.IsNullOrWhiteSpace() should be avoided for performance or compatibility reasons, either. The bottom line is, "Do I ever want to treat a string that contains nothing but whitespace as though it were a valid IP address?" and that answer is going to be, "No, that's not ever going to be a valid IP address." So code for it.
– Bacon Bits
Nov 9 at 20:50




Yeah, but if the file is ever field quoted because something changed upstream, the script will unexpectedly work differently. It's not like it's a well-documented, guaranteed behavior of Import-Csv; it's just an implementation detail. It's not like String.IsNullOrWhiteSpace() should be avoided for performance or compatibility reasons, either. The bottom line is, "Do I ever want to treat a string that contains nothing but whitespace as though it were a valid IP address?" and that answer is going to be, "No, that's not ever going to be a valid IP address." So code for it.
– Bacon Bits
Nov 9 at 20:50

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231838%2fcheck-csv-for-blank-fields-and-write-output-if-exist-blank%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo