array access with powershell

What is the correct syntax to retrieve the first or third values that are separate by space?

> gc .\abc.txt
AAA     111 333
BBB     333 444
AAA     111 333

> gc .\abc.txt|%{write-host $_[0]}
A
B
A
B

Expected output:
AAA
BBB
AAA
BBB
LVL 21
MazdajaiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
You split the string at the spaces:
gc .\abc.txt| % {$_.Split(" ", [StringSplitOptions]::RemoveEmptyEntries)[0] | write-host}

Open in new window

MazdajaiAuthor Commented:
Thanks.

In MSDN, RemoveEmptyEntries is "Split a string delimited by characters and "   "return all non-empty elements". So I can still run the following if I want to show the empty elements?

gc .\abc.txt| % {$_.Split(",")[0] | write-host}

Open in new window

oBdACommented:
Yes, that works as well; just depends on how you want to handle several consecutive spaces.
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

MazdajaiAuthor Commented:
Is there an easy way to put them all on the same line?

gc .\abc.txt| % {$_.Split(",")[0,3] | write-host}

Open in new window

oBdACommented:
(gc .\test.txt| % {$_.Split(" ")[0]}) -join " "

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MazdajaiAuthor Commented:
Quick response -  thanks a lot!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.