Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

do something for each token in a line separated by comma

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 12-17-2007   #1 (permalink)
MaxMad
Guest


 

do something for each token in a line separated by comma

Hello,

a simple problem in NT-Shell, but in Powershell i find no solution (new
in PS)

I have a Textfile where in each line are members of a group. These members
are separated by a comma. I find no way to do something with each user.
I have tried something with get-content and foreach-object.
But everything interprets the wohle line as 1 string.
If i take a textfile with each user in one line, theres no problem to handle
with.

heres a exampel for the textfile:

mary,john,paul,henry
mary,john
mary,john,paul,henry,george,hanne,anne,tom

i want to read each line and in each line do something with each user
separated with comma. The number of users in each line varies.

Pls give me some hints.

Thx and Greets
Max

My System SpecsSystem Spec
Old 12-17-2007   #2 (permalink)
Shay Levi
Guest


 

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


Hi MaxMed,

You can split method, on the comma char, on each line and then run foreach
on the result:

get-content yourfile.txt | foreach {
$users = $_.split(",")
$users | foreach { "do somthing" }
}



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


Quote:

> Hello,
>
> a simple problem in NT-Shell, but in Powershell i find no solution
> (new in PS)
>
> I have a Textfile where in each line are members of a group. These
> members
> are separated by a comma. I find no way to do something with each
> user.
> I have tried something with get-content and foreach-object.
> But everything interprets the wohle line as 1 string.
> If i take a textfile with each user in one line, theres no problem to
> handle
> with.
> heres a exampel for the textfile:
>
> mary,john,paul,henry
> mary,john
> mary,john,paul,henry,george,hanne,anne,tom
> i want to read each line and in each line do something with each user
> separated with comma. The number of users in each line varies.
>
> Pls give me some hints.
>
> Thx and Greets
> Max

My System SpecsSystem Spec
Old 12-17-2007   #3 (permalink)
MaxMad
Guest


 

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

Hi Shay Levi :-)

Thanks a lot! It works!!
One more Problem: Is there a posibility to remove spaces in each token?

Greets
MaxMad

"Shay Levi" wrote:
Quote:

>
> Hi MaxMed,
>
> You can split method, on the comma char, on each line and then run foreach
> on the result:
>
> get-content yourfile.txt | foreach {
> $users = $_.split(",")
> $users | foreach { "do somthing" }
> }
>
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>
>
>
Quote:

> > Hello,
> >
> > a simple problem in NT-Shell, but in Powershell i find no solution
> > (new in PS)
> >
> > I have a Textfile where in each line are members of a group. These
> > members
> > are separated by a comma. I find no way to do something with each
> > user.
> > I have tried something with get-content and foreach-object.
> > But everything interprets the wohle line as 1 string.
> > If i take a textfile with each user in one line, theres no problem to
> > handle
> > with.
> > heres a exampel for the textfile:
> >
> > mary,john,paul,henry
> > mary,john
> > mary,john,paul,henry,george,hanne,anne,tom
> > i want to read each line and in each line do something with each user
> > separated with comma. The number of users in each line varies.
> >
> > Pls give me some hints.
> >
> > Thx and Greets
> > Max
>
>
>
My System SpecsSystem Spec
Old 12-17-2007   #4 (permalink)
Shay Levi
Guest


 

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


The string type has three methods to trim white space characters (or set
of characters) from a string, you can get them by running:

PS > " just a string " | gm tr*


TypeName: System.String

Name MemberType Definition
---- ---------- ----------
Trim Method System.String Trim(Params Char[] trimChars), System.String
Trim()
TrimEnd Method System.String TrimEnd(Params Char[] trimChars)
TrimStart Method System.String TrimStart(Params Char[] trimChars)



Trim - Removes all trailing occurrences of a set of characters specified
in an array from the current String object.
TrimEnd - Removes all trailing occurrences of a set of characters specified
in an array from the current String object.
TrimStart - Removes all leading occurrences of a set of characters specified
in an array from the current String object.


# remove leading and trailing spaces from a string
PS > " just a string ".trim()
just a string



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


Quote:

> Hi Shay Levi :-)
>
> Thanks a lot! It works!! One more Problem: Is there a posibility to
> remove spaces in each token?
>
> Greets
> MaxMad
> "Shay Levi" wrote:
>
Quote:

>> Hi MaxMed,
>>
>> You can split method, on the comma char, on each line and then run
>> foreach on the result:
>>
>> get-content yourfile.txt | foreach {
>> $users = $_.split(",")
>> $users | foreach { "do somthing" }
>> }
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>>> Hello,
>>>
>>> a simple problem in NT-Shell, but in Powershell i find no solution
>>> (new in PS)
>>>
>>> I have a Textfile where in each line are members of a group. These
>>> members
>>> are separated by a comma. I find no way to do something with each
>>> user.
>>> I have tried something with get-content and foreach-object.
>>> But everything interprets the wohle line as 1 string.
>>> If i take a textfile with each user in one line, theres no problem
>>> to
>>> handle
>>> with.
>>> heres a exampel for the textfile:
>>> mary,john,paul,henry
>>> mary,john
>>> mary,john,paul,henry,george,hanne,anne,tom
>>> i want to read each line and in each line do something with each
>>> user
>>> separated with comma. The number of users in each line varies.
>>> Pls give me some hints.
>>>
>>> Thx and Greets
>>> Max

My System SpecsSystem Spec
Old 01-09-2008   #5 (permalink)
Newbie


Join Date: Jan 2008
Vista Business 32bit
 
Rep Power: 6
Powershellnewbie is on a distinguished road
  Powershellnewbie is offline

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

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@abccompany.com,janedoe@abcco...abccompany.com,

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
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
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert CSV semi-colone to comma separated Gregor PowerShell 3 04-10-2008 09:25 AM
Search fails if Folder name has comma in it? NickT Vista file management 1 01-15-2008 05:59 AM
end of line while processing a string token issue hectoritnt PowerShell 6 09-28-2007 01:22 PM
Argument that starts with '-' and contains ':' gets separated. Dan PowerShell 4 05-15-2007 01:50 PM
Encountered end of line while processing a string token Marco Shaw PowerShell 5 11-15-2006 06:36 AM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51