0

I'm splitting a csv on the comma and taking the 6th cell:

Get-Content .\BSEG_EXPORT.csv | foreach { $_.Split(",")[6] }

However, this returns a lot of empty strings (no match on the current line):

""
""
"something" 
""
""

How can I ommit the "" in my output? So:

"something" 

I tried

Get-Content .\BSEG_EXPORT.csv | foreach { $a = $_.Split(",")[6] } | Where { $a -ne "" }

but this does not work.

2 Answers 2

1

What about:

Get-Content .\BSEG_EXPORT.csv | foreach { 
  $cell = $_.Split(",")[6] 
  if ($cell -ne '""' ) { write-output $cell }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but trying this on the commandline (Get-Content .\BSEG_EXPORT.csv | foreach { $cell = $_.Split(",")[6] if ($cell -ne "") { Write-Host $cell } }) gives me an error: Unexpected token if. I can probably put it in a .ps1 and it'll work, but how do I formet this for the command line?
add a semi colon between the $cell assignment and the if if you're doing it all on one line: $cell = $_.Split(",")[6] ; if ($cell ...) {... }
1

Try it this way:

Get-Content .\BSEG_EXPORT.csv | foreach { $_.Split(",")[6] -ne '""'}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.