INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

String to Array

String to Array

(OP)
Hi all I am new to powershell and have a beginner question. I have a variable that is storing a bunch of strings based on a Select-string command-let I use.

CODE --> PowerShell

$results | Select-String 'port' 

Results in:
IP:port : 0.0.0.0:13125
IP:port : 0.0.0.0:13126
IP:port : 0.0.0.0:13128
IP:port : 0.0.0.0:13130
IP:port : 0.0.0.0:13131
IP:port : 0.0.0.0:13132
IP:port : 0.0.0.0:13133
IP:port : 0.0.0.0:13134
IP:port : 0.0.0.0:13137

How can I extract/trim only the port numbers, for example 13135 , and store them in an array so that I can use later on. I was thinking TRIMEND but that is not working. I am not familiar with all the methods of string.
Thanks in advance.

CODE --> Powershell

TypeName: System.String

Name             MemberType            Definition
----             ----------            ----------
Clone            Method                System.Object Clone()
CompareTo        Method                int CompareTo(System.Object value), int CompareTo(string strB)
Contains         Method                bool Contains(string value)
CopyTo           Method                System.Void CopyTo(int sourceIndex, char[] destination, int destinationIndex,...
EndsWith         Method                bool EndsWith(string value), bool EndsWith(string value, System.StringCompari...
Equals           Method                bool Equals(System.Object obj), bool Equals(string value), bool Equals(string...
GetEnumerator    Method                System.CharEnumerator GetEnumerator()
GetHashCode      Method                int GetHashCode()
GetType          Method                type GetType()
GetTypeCode      Method                System.TypeCode GetTypeCode()
IndexOf          Method                int IndexOf(char value), int IndexOf(char value, int startIndex), int IndexOf...
IndexOfAny       Method                int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int startIndex), i...
Insert           Method                string Insert(int startIndex, string value)
IsNormalized     Method                bool IsNormalized(), bool IsNormalized(System.Text.NormalizationForm normaliz...
LastIndexOf      Method                int LastIndexOf(char value), int LastIndexOf(char value, int startIndex), int...
LastIndexOfAny   Method                int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] anyOf, int startI...
Normalize        Method                string Normalize(), string Normalize(System.Text.NormalizationForm normalizat...
PadLeft          Method                string PadLeft(int totalWidth), string PadLeft(int totalWidth, char paddingChar)
PadRight         Method                string PadRight(int totalWidth), string PadRight(int totalWidth, char padding...
Remove           Method                string Remove(int startIndex, int count), string Remove(int startIndex)
Replace          Method                string Replace(char oldChar, char newChar), string Replace(string oldValue, s...
Split            Method                string[] Split(Params char[] separator), string[] Split(char[] separator, int...
StartsWith       Method                bool StartsWith(string value), bool StartsWith(string value, System.StringCom...
Substring        Method                string Substring(int startIndex), string Substring(int startIndex, int length)
ToCharArray      Method                char[] ToCharArray(), char[] ToCharArray(int startIndex, int length)
ToLower          Method                string ToLower(), string ToLower(System.Globalization.CultureInfo culture)
ToLowerInvariant Method                string ToLowerInvariant()
ToString         Method                string ToString(), string ToString(System.IFormatProvider provider)
ToUpper          Method                string ToUpper(), string ToUpper(System.Globalization.CultureInfo culture)
ToUpperInvariant Method                string ToUpperInvariant()
Trim             Method                string Trim(Params char[] trimChars), string Trim()
TrimEnd          Method                string TrimEnd(Params char[] trimChars)
TrimStart        Method                string TrimStart(Params char[] trimChars)
Chars            ParameterizedProperty char Chars(int index) {get;}
Length           Property              System.Int32 Length {get;} 

RE: String to Array

First here is how to isolate the value you want from each line.

CODE --> PowerShell

$Line = "IP:port : 0.0.0.0:13125"
$Port = $Line.Split(":")
$Port[3] 

You can get the individual lines with a ForEach like this:

CODE --> PowerShell

$Line = "IP:port : 0.0.0.0:13125"
$Port = $Line.Split(":")
$Port[3]

ForEach ($Line in $Results)
{
$Port = $Line.Split(":")
$Port[3]
} 

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at http://www.thespidersparlor.com/vbscript

Work SMARTER not HARDER.

RE: String to Array

(OP)
Mark thanks for the tip. I think I was over thinking it.


ports.txt contains
IP:port : 0.0.0.0:13125
IP:port : 0.0.0.0:13126
IP:port : 0.0.0.0:13128
IP:port : 0.0.0.0:13130
IP:port : 0.0.0.0:13131
IP:port : 0.0.0.0:13132
IP:port : 0.0.0.0:13133
IP:port : 0.0.0.0:13134
IP:port : 0.0.0.0:13137

CODE --> Powershell

$Results = Get-Content c:\ports.txt

ForEach ($Line in $Results)
{
$Port = $line.Split(":")
$Port[3]
} 

RE: String to Array

Happy to help. Please check out my FAQs in this forum, I would appreciate if you could rate them. They might help you learn a few tricks too.

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at http://www.thespidersparlor.com/vbscript

Work SMARTER not HARDER.

RE: String to Array

(OP)
Sure thing. I rated two yesterday. Set spn is sweet!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close