View Single Post
Old 01-09-2008   #6 (permalink)
Shay Levi
Guest


 
 

Re: do something for each token in a line separated by comma



$s = "johndoe@xxxxxx,janedoe@xxxxxx,xyzuser@xxxxxx,somebody@xxxxxx,"
[regex]::matches($s,',?(?<name>[^@]+)@[^\.]+\.\w+') | foreach { $_.groups['name'].value
}

johndoe
janedoe
xyzuser
somebody



To process a file:

get-content <file> | foreach {
[regex]::matches($_, ',?(?<name>[^@]+)@[^\.]+\.\w+') | foreach { $_.groups['name'].value
}
}



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Hello,
>
> I have a very similar issue that is fairly straight forward with a
> batch file, but I would like to learn a simpler cleaner version with
> Powershell.
>
> We have text files that look like this -
>
> johndoe@xxxxxx,janedoe@xxxxxx,xyzuser@xxxxxx,s
> omebody@xxxxxx,
>
> etc.
>
> all on one line of text. We want to just strip out the username
> (everything before the @ symbol) and nothing more then put them into a
> new text file 1 per line. (or to excel, since Powershell can do this)
> The problem that I have been encountering is that when I try something
> like this -
>
> $a = Get-Content ("\\network location\long_names.txt") $b =
> $a.split('@')
>
> I get a result that looks like this -
>
> johndoe
> abccompany.com,janedoe
> abccompany.com,xyzuser
> abccompany.com,somebody
> etc.
>
> Is there a way to only get the username and put it into new file with
> one username per line?
>
> I am very new to PowerShell but am starting a 20 hour course, so
> hopefully I'll know more soon. Any help would be appreciated. We
> have already accomplished this with a batch file that we wrote up in
> 30 mins or so, but it would be nice to see these real world examples
> able to be accomplished with Powershell.
>
> Thanks!
>

My System SpecsSystem Spec