Link to home
Start Free TrialLog in
Avatar of JJENSEN3
JJENSEN3

asked on

Parsing and Extracting Data From A Text File In PowerShell

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")
Avatar of oBdA
oBdA

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

Avatar of JJENSEN3

ASKER

I am trying your "straight-forward" suggestion, but it is returning a null value.
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

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks so much for the clarification. There was a null second line. Your revised code took care of the issue.
Thank you.