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|Compa ny name|Name|Home|Office|Cell ular|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\Documen ts\ Files\Breeze.kit|HJO PO|MC17R1|RAL.MP.S - Ralston Maple Square|No Option|||Ralston Maple
Initial Read Command:
$content = [IO.File]::ReadAllText($di rectorypat h + "\Sample File.txt")
File Data:
HT|Dealer ID|Comment|Contact Name|Salesman|Dealer|Compa
HI||1152678|||||, ||||||,|,|C:\Users\Documen
Initial Read Command:
$content = [IO.File]::ReadAllText($di
ASKER
I am trying your "straight-forward" suggestion, but it is returning a null value.
I was testing with this:
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks so much for the clarification. There was a null second line. Your revised code took care of the issue.
Thank you.
Thank you.
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:Open in new window