Powershell Script to remove rows from a CSV

jamiegf
jamiegf used Ask the Experts™
on
Hi all.
I would like a script to remove all lines\rows from a .CSV log where an entry of "0" is found in the 3rd column. The csv does not have headers.
Does anyone know how I can do this using powershell?

Thanks very much.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
You need to add as many "ColX" to the Header argument of Import-Csv as the file has columns; the scripts below assume 3.
If you want/need the fields enclosed in double quotes:
Import-Csv -Path C:\Temp\input.csv -Header Col1, Col2, Col3 |
	Where-Object {$_.Col3 -ne '0'} |
	ConvertTo-Csv -NoTypeInformation |
	Select-Object -Skip 1 |
	Set-Content -Path C:\Temp\output.csv

Open in new window

Without quotes:
Import-Csv -Path C:\Temp\input.csv -Header Col1, Col2, Col3 |
	Where-Object {$_.Col3 -ne '0'} |
	ConvertTo-Csv -NoTypeInformation |
	Select-Object -Skip 1 |
	ForEach-Object {$_.Replace('"', '')} |
	Set-Content -Path C:\Temp\output.csv

Open in new window

Author

Commented:
You absolute legend! thanks very much.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial