Search This Blog

Monday, February 07, 2011

Regular Expression: match whole line

I needed to match find a string within  multiple text files. I used this expression to do it. The line started with stringtomatch, but you could change to search within a line by prefixing .*

^stringtomatch.*$

^ – caret matches start of line

$ – dollar matches end of line

stringtomatch - string to match at the start of the line, if its in the middle you could prefix .* (i.e     .*stringtomatch.*    )

.* - matches any characters


Share/Bookmark

Sunday, February 06, 2011

Powershell– Comma Operator and Percentage (%) Alias

Ok, I have been dabbling more into the world of PowerShell, and wanted to note a couple of things I have come across.

% (Percentage) Alias

I have seen % used before bracketed statements.

% ($banana), basically the percentage is short hand for ForEach-Object ()

Get-Content ServerList.txt | ForEach-Object{,($_.split(','))

or

Get-Content ServerList.txt | %{,($_.split(','))

The line above gets the contents of the file serverlist.tzt and pipes it. Then ForEach-Object loops through each object passed (in this case it will be each line read from the file.

Comma Operator

In doing the above command splitting the array and piping it, I had issues displaying the array elements. I came across this post which helps explain what's going.

http://winpowershell.blogspot.com/2006/08/passing-arrays-through-pipeline.html

But the author used ,() to get round the issue and I wondered what this was doing.

,($_.Split(" "))

In the end I think all the ,() is doing is putting the object into a single element array which stops PowerShell flattening out the array. I base my understanding on this page from MS.

http://technet.microsoft.com/en-us/library/dd347588.aspx


Share/Bookmark