Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Reading Pipe Delimited Files in Powershell

  1. Jun 23, 2012 #1


    User Avatar

    I am trying to learn Powershell and for my first exercise I started reading a file that we use at work. The file is pipe delimited and contains a header row. The field names contain underscores. Here is the header row (wrapped to fit this window).

    H|Seq_Nbr|Data_Key |Plan_Type |Mod_Type |Plan_Name |Mod_Name |Plan_Desc |Mbr_T |Mbr_A |Mbr_B |Mbr_C |Mbr_D

    The values of Data_Key are dot delimited. A typical value would be like "Planned.PlanMast.BU29" the rest of the values are strings without any punctuation marks. The last two fields are allowed to be empty.

    To read the file in I've used the Powershell import-csv cmdlet like this:

    file$ = import-csv c:\imp\ssw\admin\tbl_list_planned.tbl -Delimiter '|'

    So far so good. Next I want to return three fields according to whether or not one of the fields meets a certain criteria. Breaking this in parts, I sought first just to get the three fields I require. The next two lines seem to do the trick:

    $fields = $file[0].psobject.properties | %{$_.name}
    $file | select $fields[2,4,5]

    So now I know how to get just the three fields I want. The last step is to pare this result down to just those records where field 4 (mod_type) is some value I care about. I've had no success doing this going down the road we're on. I've tried:

    $rows = $file | select $fields[2,4,5]| where {$_ -like "BUPP29"}

    I've tried:

    $file = import-csv c:\imp\ssw\admin\tbl_list_planned.tbl -Delimiter '|' | where-object {$_.mod_type -eq "BUPP29"}

    I tried:

    import-csv C:\imp\ssw\admin\tbl_list_Planned.tbl -Delimiter "|" | % { foreach ($s in $_) { $s.split.("|")[4] } }

    I'm floundering around and haven't found anything on the web. Thanks in advance to anyone who can give me a pointer.
  2. jcsd
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted