Reading Pipe Delimited Files in Powershell

  • Thread starter MLP
  • Start date


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] | %{$}
$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.

Want to reply to this thread?

"Reading Pipe Delimited Files in Powershell" You must log in or register to reply here.

Related Threads for: Reading Pipe Delimited Files in Powershell


Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving