none
Look for a word in CSV and remove it and everything below it RRS feed

  • Question

  • I am importing a CSV file and I need to do 2 things with it.

    $myPCList = (  Import-Csv "$env:TEMP\csvfiles\file_computers.csv")


    Remove any blank lines and remove the line containing the word example and every line of text below it.<o:p></o:p>

    I am sure I can use an If statement and trim but not sure how to do it.<o:p></o:p>

    I cannot  insert a image so i hope this is enough info.

    Thanks

    Monday, January 19, 2015 3:15 AM

Answers

  • $InputFile = "$env:TEMP\csvfiles\file_computers.csv"
    $OutputFile = "$env:TEMP\csvfiles\file_computers2.csv"
    $StopLine = "Examples:,,,,,,,,,,,,,,,"
    Get-Content $InputFile | % {If ($_ -ne $StopLine) {$_} Else {Break}} | Set-Content $OutputFile
    • Marked as answer by Lishron Monday, January 19, 2015 1:29 PM
    Monday, January 19, 2015 1:29 PM

All replies

  • A CSVcannot have blank lines orr it will not load.

    You can detect a line like this:

    Get-Content | ?{$_ -contains 'the word example'}

    Since we would not want you to hurt yourself using these advanced tools I recommend that you contact a trained technician to perform such delicate extractions.

    As it says on the box "No user serviceable parts inside!"


    ¯\_(ツ)_/¯

    Monday, January 19, 2015 4:31 AM
  • Thanks very much.

    I will make the solution and share it.

    Monday, January 19, 2015 12:03 PM
  • A CSVcannot have blank lines orr it will not load.

    You can detect a line like this:

    Get-Content | ?{$_ -contains 'the word example'}

    Since we would not want you to hurt yourself using these advanced tools I recommend that you contact a trained technician to perform such delicate extractions.

    As it says on the box "No user serviceable parts inside!"


    ¯\_(ツ)_/¯

    If anyone really uses this site anymore other then me and those wanting to ridicule those like me a CSV CAN have blank rows.


    Monday, January 19, 2015 12:22 PM
  • Teasing is nto really ridiculing.  A CSV that has blank lines is broken.

    To see what I mean place the follwoing in a file (test.csv) then import it:

    A,B,C
    1,2,3
    
    4,5,6
    

    $csv=import-csv test.csv

    Now count the lines:
    $csv.Count

    Notice no blank lines.

    If you try to import the CSV into most databases the immport utility willthrow an error on blank lines.

    Just because you can create a file with a CSV extension that has blank lines does not make it a legal or usable CSV file.  The CSV file say no blank lines

    This, however, is legal:

    A,B,C
    1,2,3
    ,,
    4,5,6
    
    Now it will import correctly.


    ¯\_(ツ)_/¯

    Monday, January 19, 2015 12:59 PM
  • To filter a CSV for blank fields we would do this:

    Import-Csv test.csv | ?{$_.A -or $_.B -or $_.C}

    If all fields are blank the row is skipped.


    ¯\_(ツ)_/¯

    Monday, January 19, 2015 1:03 PM
  • $InputFile = "$env:TEMP\csvfiles\file_computers.csv"
    $OutputFile = "$env:TEMP\csvfiles\file_computers2.csv"
    $StopLine = "Examples:,,,,,,,,,,,,,,,"
    Get-Content $InputFile | % {If ($_ -ne $StopLine) {$_} Else {Break}} | Set-Content $OutputFile
    • Marked as answer by Lishron Monday, January 19, 2015 1:29 PM
    Monday, January 19, 2015 1:29 PM
  • Thanks very much  

    Monday, January 19, 2015 1:30 PM
  • Not really the quesiton you asked.  It is a text file as used andnot really a CSV.

    ¯\_(ツ)_/¯

    Monday, January 19, 2015 1:32 PM