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?
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

0
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

0
oBdACommented:
Yes, that works as well; just depends on how you want to handle several consecutive spaces.
0
How do you know if your security is working?

Protecting your business doesn’t have to mean sifting through endless alerts and notifications. With WatchGuard Total Security Suite, you can feel confident that your business is secure, meaning you can get back to the things that have been sitting on your to-do list.

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

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

Open in new window

0

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!
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.