Parsing and Extracting Data From A Text File In PowerShell

JJENSEN3
JJENSEN3 used Ask the Experts™
on
I have a Powershell script that reads the full content of an external data file. The file contains one line of header fields and the second line contains the associated data to those fields. My question is- how would I extract one of the fields in that file, in this case the "PO Number" field, and assign it to a variable in powershell? Below is the example of the file content and the command in Powershell to read the file.

File Data:
HT|Dealer ID|Comment|Contact Name|Salesman|Dealer|Company name|Name|Home|Office|Cellular|Ship to|Address|Address|Address|File Name|PO Number|Catalog Name|Wall Door Style|Drawer Front|Drawer Pull|Door Pull|Price Level|

HI||1152678|||||, ||||||,|,|C:\Users\Documents\ Files\Breeze.kit|HJO PO|MC17R1|RAL.MP.S - Ralston Maple Square|No Option|||Ralston Maple

Initial Read Command:
$content = [IO.File]::ReadAllText($directorypath + "\Sample File.txt")
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
There's always the straight-forward way: count and split; the field in question is the 17th, so you need the zero-based index 16:
$inFile = Join-Path -Path $directorypath -ChildPath "\Sample File.txt"
$poNumber = (Get-Content -Path $inFile | Select-Object -Index 1).Split('|')[16]

Open in new window

Or the more versatile one, where you make the data csv compatible (multiple "Address" columns, empty last column), and then import it as csv with access to the properties:
$inFile = Join-Path -Path $directorypath -ChildPath "\Sample File.txt"
$i = $j = 1
$header = (Get-Content -Path $inFile -TotalCount 1).Split('|') | ForEach-Object {If ($_ -eq 'Address') {"$($_)$(($i++))"} ElseIf (-not $_) {"null$(($j++))"} Else {$_}}
$csv = Get-Content -Path $inFile | Select-Object -Skip 1 | ConvertFrom-Csv -Header $header -Delimiter '|'
$poNumber = $csv.'PO Number'

Open in new window

Author

Commented:
I am trying your "straight-forward" suggestion, but it is returning a null value.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
I was testing with this:
HT|Dealer ID|Comment|Contact Name|Salesman|Dealer|Company name|Name|Home|Office|Cellular|Ship to|Address|Address|Address|File Name|PO Number|Catalog Name|Wall Door Style|Drawer Front|Drawer Pull|Door Pull|Price Level|
HI||1152678|||||, ||||||,|,|C:\Users\Documents\ Files\Breeze.kit|HJO PO|MC17R1|RAL.MP.S - Ralston Maple Square|No Option|||Ralston Maple

Open in new window


Or is there an actual empty second line in the data?
HT|Dealer ID|Comment|Contact Name|Salesman|Dealer|Company name|Name|Home|Office|Cellular|Ship to|Address|Address|Address|File Name|PO Number|Catalog Name|Wall Door Style|Drawer Front|Drawer Pull|Door Pull|Price Level|

HI||1152678|||||, ||||||,|,|C:\Users\Documents\ Files\Breeze.kit|HJO PO|MC17R1|RAL.MP.S - Ralston Maple Square|No Option|||Ralston Maple

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Assuming there is one (or more) empty lines after the header, straight-forward would be
$inFile = Join-Path -Path $directorypath -ChildPath "\Sample File.txt"
$poNumber =  (Get-Content -Path $inFile | Where-Object {$_} | Select-Object -Index 1).Split('|')[16]

Open in new window

ConvertTo-Csv doesn't care about empty lines.

Author

Commented:
Thanks so much for the clarification. There was a null second line. Your revised code took care of the issue.
Thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial