Search code examples
powershellpowershell-3.0

Powershell Import-CSV how to skip until particular line based on string?


Hi My CSV file is like this

#BEGINPROPERTIES
total.candidate.create=2
duration=0:00:00.433
internal.audit.session.id=1397055568
internal.cluster.node.info=(product:enterprise,id:node796599_796601,http:"10.111.233.79:19788",jgroups:"chprapk11406-44186",contextPath:/enterprise,isCoordinator:false)
processing.batching.size=1
total.candidate=2
non.updatable.fields=error.different
internal.cluster.node.id=node796599_796601
progress.interval=1
CSVLineCount=2
default.operation=merge
total.status.error=2
#ENDPROPERTIES
"Index","Identifier","Status","TransactionType","Result","Message"
"1","Candidate|create|224568907","error","candidate.create",,"The following email address is already used by a candidate included in the database: [email protected].;"

While importing CSV in powershell, I need to consider only from Column name ( "Index") and need to skip till #ENDPROPERTIES". There are no defined count for lines as each time the count may vary but every time I need to skip until "#ENDPROPERTIES". How do I achieve that?


Solution

  • Something like this:

    $csvpath = "C:\deleteme.csv"
    $csvContent = Get-Content -Path $csvpath 
    $LineNumber = $csvContent | Select-String '#ENDPROPERTIES' | Select-Object -ExpandProperty 'LineNumber'
    $csvData = $csvContent | Select-Object -Skip $LineNumber | ConvertFrom-Csv