Reading Pipe Delimited Files in Powershell

In summary, The file contains a header row with fields that have underscores in them. The fields are Seq_Nbr, Data_Key, Plan_Type, Mod_Type, Plan_Name, Mod_Name, and Plan_Desc. 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. The first line of the file is the header row. The next lines seem to import the data into the variable file$. The third line seems to do
  • #1
MLP
32
0
Hello,
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.
 
Technology news on Phys.org
  • #2
The answer to your question is: $file = import-csv c:\imp\ssw\admin\tbl_list_planned.tbl -Delimiter '|' | Where-Object {$_.'Mod_Type' -eq "BUPP29" } | Select-Object $fields[2],$fields[4],$fields[5]
 

1. What is a pipe delimited file?

A pipe delimited file is a type of text file where the data is separated by the pipe character, "|". It is a common format used for storing and exchanging data in a structured manner.

2. How do I read a pipe delimited file in Powershell?

To read a pipe delimited file in Powershell, you can use the Import-Csv cmdlet and specify the delimiter as "|". This will allow you to access the data in the file as an array of objects.

3. Can I modify the data in a pipe delimited file using Powershell?

Yes, you can modify the data in a pipe delimited file using Powershell. Once you have imported the data as an array of objects, you can use various cmdlets and operators to manipulate the data and then export it back to the file.

4. Are there any limitations to reading pipe delimited files in Powershell?

One limitation to keep in mind when reading pipe delimited files in Powershell is that the file must have a consistent structure. This means that all rows in the file must have the same number of columns and the data must be delimited in the same way throughout the file.

5. Can I use Powershell to read a pipe delimited file from a remote location?

Yes, you can use Powershell to read a pipe delimited file from a remote location by using the Invoke-Command cmdlet. This will allow you to run commands on a remote computer and retrieve the data from the pipe delimited file on that computer.

Similar threads

  • Programming and Computer Science
Replies
2
Views
21K
  • Programming and Computer Science
Replies
6
Views
974
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • Programming and Computer Science
Replies
8
Views
5K
  • Programming and Computer Science
Replies
9
Views
3K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
12K
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
2
Views
3K
Back
Top