Showing posts with label text. Show all posts
Showing posts with label text. Show all posts

Monday, October 15, 2012

Let’s select the String using Select-String in Powershell.

Hi,

In my previous post i have write a little article on how to view to see the contents of files.

As you know that i like Text files and my most of the day spent on PowerShell console so i love to do different different examples with cmdlets and command.

On my laptop i have a simple test file name as “Seen Movies.txt”, i this i mainly put the name of all movies which i have seen on my laptop and whose video files i do have stored on my NAS drive. SO when i need to get any movie i check this file to know that if i already have that movie or not. In case i have a movies entry in the file then i don’t get or purchase that Movies and if i don’t have entry of that movie then i can think of buying it if i am interested to watch that movie.

So what’s special ? “we can do the same by open the file in notepad and then we can search for that movie”.

Yes agreed but remember there are lots of way to do one thing, Smile

To check that i have that movie or not i just run a simple command.

Get-Content FILE_NAME | Select-String “Movie Name”

Using Get-content we are getting the content of the files and the we are piping the output to Select-String Cmdlet and then selecting to show the string “Avenger”

12-10-2012 00-16-01

The above command showed me that i have “The Avengers” movies in my database.

ok but what is the real use?

The real use is that you can search for specific words in log files.

Get-Content C:\Windows\Logs\CBS\CBS.log | Select-String "error"

In above command i am getting the content of C:\Windows\Logs\CBS\CBS.log  and piped them to the Select-String cmdlet and asking it match everything which contain the word “Error”

12-10-2012 00-35-39

If you want to do a search on case sensitive words then you can use -CaseSensitive parameter with Select-String cmdlet

12-10-2012 00-39-39

Simple isn’t Smile  and this is very handy and useful cmdlet Smile .

 

Thanks

Aman Dhally

join aman on facebook Join aman on Linkedin follow aman on Twitter

Monday, February 13, 2012

Part-6: Text Manipulation in PowerShell using .ToCharArray(),.PadLeft(), and .PadRight() methods

 

Part-1: Text Manipulation in PowerShell using Trim, TrimStart , TrimEnd methods
Part-2: Text Manipulation in PowerShell using .ToLower and .ToUpper method
Part-3: Text Manipulation in PowerShell using .StartsWith() and EndsWith() methods
Part-4: Text Manipulation in PowerShell using .Replace(), and .Remove() methods.
Part-5: Text Manipulation in PowerShell using .Contains() and .Equals() methods

Hi,

Today we are going to see what .Chars() , PadLeft() and Padright() does. And I think after covering this method there are only few methods left to cover for the next text manipulation series articles.

We are to using $text variable to show the mentioned methods.

 

$text = "The quick Brown fox"

13-02-2012 11-32-49

ToCharArray()

 

The .Chars() method convert the whole string to in to the array.

$a = $text.ToCharArray()

 

As you can see in the below screenshot it spilt the whole string in to the single character. This is converted in to a array object.

13-02-2012 11-37-23

How to check this is an array?

Okay! Let’s assign the above command to a another variable

$arr = $text.ToCharArray()

13-02-2012 11-41-23 

And now try to access the first object of array,the output is "T" and when we try to access the second object in the array the output is "H"

$arr[0]

$arr[1]

 

13-02-2012 11-44-03 

 

 

PadLeft()

 

The PadLeft() method basically added the specified number of space on the left of the sting, we need to specify how many black space we want to add in enclosed with bracket.

 

$text.PadLeft(200)

You can clearly see the blank spaces in the front of our original text.

 

13-02-2012 11-49-39

.PadRight()

The .PadRight() does the same thing but I added the specified number of space to the right side of the string.

 

Thanks for reading :)

Aman Dhally