View Single Post
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