I have a text file with the following format:
Rtmp: 2a1234bzcde9 SL ID: 1549566 IP: 192.168.0.1
Rtmp: 45a1234b4erde9 SL ID: 1549566 IP: 192.168.0.1
Rtmp: a1254bcde9 SL ID: 1549566 IP: 192.168.0.1
Rtmp: 23a1ft4bcde9 SL ID: 1549566 IP: 192.168.0.1
Rtmp: a125egbcde9 SL ID: 1549566 IP: 192.168.0.1
I have several hundred entries involved here.
The problem is that I need to get the second entry in each line of text (inbetween rtmp and sl). Each of those numbers has a length between 6 to 15 characters, all random characters.
How would I pull it into an array of those numbers? I want to use these numbers to make them the names of user accounts in 2008r2 (not using AD) and create folders in the inetpub/ftproot and create the folders in there with aliases and link each account to its corresponding virtual folder.
The last bit I can do.. it's manipulating text files that I suck at!! Here's what I've got so far:
$items = Get-Content $HOME/desktop/info.txt
$splitItems = @()
$splitItems = foreach ($item in $items)
{
$item.split(" ")
}
That splits each line, so that splitItems[0] is the first line of text, now split into multiple lines of text because of the space delimiter.
If I tried to take the SplitItems aray and use the same type of foreach to further split it. It gave me back an array of chars. USEFUL LOL haha. well.. each way I try I keep getting mumbo jumbo or it's not开发者_Python百科 a string type (though get type seems to say it's a string). I think in the process the string type changes to a generic io.object type?
Any ideas or help would be immensely appreciated.!!!
The regex answers may be the right way to go, but if your input file is really as consistently formatted as you suggest, you may have been on the right track to begin with. Try something like this:
$accounts = gc info.txt | % { ($_ -split ' ')[1] }
$accounts
Given your text file is "c:\temp\t.txt", can you test this :
switch -regex -file "c:\temp\t.txt"
{
"^Rtmp: (.*) SL.*$" {$matches[1]}
}
$a = "Rtmp: 2a1234bzcde9 SL ID: 1549566 IP: 192.168.0.1 Rtmp: 45a1234b4erde9 SL ID: 1549566 IP: 192.168.0.1 Rtmp: a1254bcde9 SL ID: 1549566 IP: 192.168.0.1 Rtmp: 23a1ft4bcde9 SL ID: 1549566 IP: 192.168.0.1 Rtmp: a125egbcde9 SL ID: 1549566 IP: 192.168.0.1"
[regex]$regex = "Rtmp:\s(\S+)\sSL"
[regex]::matches($a,$regex) | foreach-object {$_.groups[1].value}
2a1234bzcde9
45a1234b4erde9
a1254bcde9
23a1ft4bcde9
a125egbcde9
If you want to extract one part from a string, and this string is following a defined pattern, simply use '-replace' and a regular expression.
$numbers = ($items -replace '^Rtmp: (\S+) SL.*$','$1')
This line will iterate through each item, extract the string and output it to the new array.
Some meanings:
- \S+ means "One or more characters that are not whitespace"
- $1 means "The part within the first set of brackets"
精彩评论