1

I'm new to Powershell and need help in extracting table data that is there in a plain text file.

The plain text file data is in the below format.(Tab Separated)

Header1 Header2 Header3

Val1 Val2 Val3

Now, I want to extract this data and populate the variables like below.

 $Header1 = "Val1"
 $Header2 = "Val2"
 $Header3 = "Val3"

Please help me with the right approach.

4
  • 2
    Have you considered using Import-Csv ? You may explain the actual task - not the approach you think you need to accomplish this task. ;-) Commented Apr 5, 2020 at 18:33
  • Hi Olaf, the source file is not a csv in fact. The actual task for me is just to populate the header variables like I've shown in the question. Commented Apr 5, 2020 at 18:50
  • 1
    Yes, it really is a csv file, only it uses tab instead of comma as the delimiter. Take a look at the delimiter parameter in Import-csv. Commented Apr 5, 2020 at 19:03
  • Thank you @WalterMitty, I wasn't aware of that, yeah you and Olaf were right. I just started learning PS and had a different idea. Commented Apr 6, 2020 at 2:29

1 Answer 1

4

Basically you need to import-csv and use the -delimeter parameter and specify a tab as shown below

PS C:\Temp> $g = import-csv textfilewithtabs.txt -Delimiter "`t"

PS C:\Temp> $g

header1 header2 header3
------- ------- -------
value1  value2  value3 


PS C:\Temp> $g | select header2

header2
-------
value2 

PS C:\Temp> $g.header1
value1

PS C:\Temp> $g.header2
value2
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Itchydon, this worked like charm! Thank you very much! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.