Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

Hey, powerShell Guy !How Can I Locate a Word in a Text File and Then Echo Back That Word and Any Remaining Text?

Another translation of an example script from a Hey scripting guy column How Can I Locate a Word in a Text File and Then Echo Back That Word and Any Remaining Text? from VbScript to PowerShell .

I made these 2 different versions within minutes : 

Get-Content test.txt | out-string |% {$_.substring($_.IndexOf('telescope'))}

and

$Contents = [io.file]::ReadAllText('c:\scripts\test.txt')
$Index = $Contents.IndexOf('telescope')
$Contents.Substring($index)

I first use the Index of method to find the position of the word and then use the Substring method to get the text from there on.

The use of the pipeline and foreach (%) in the first example might seem a bit strange but is very handy in interactive use, the second example using variables is a but more clear and shows how to use .NET directly, you also can use get-content here like this :

$Contents = Get-Content test.txt | out-string

As PowerShell is very flexible you have a lot of options as TMTOWTDI , what is best depends on the usage scenario

 Using aliases and using the pipeline is great for interactive shell use, and help getting things done quickly on the console, but as I also discussed here : PowerShell examples used on ars technica about two scripts from former Hey PowerShell guy posts used on ars technica, using oneliners can also be confusing.

For the second example by only seeing the code, it would be easier to figure out what the script does does as from the first oneliner example. 

You can see in the session output below , how I came to this 2 versions

note :

  • the out-string when using get-content is needed as it returns a array of lines and we want it is one string
  • I use two different way's to combine the 2 commands and to read the text only once
  • the foreach (%) in the first example is used as a shortcut to get the text in a variable ($_)
  • when using the .NET classses directly we do not need out-string but we need to provide a full path
  • extra lines are added when using get-content

 

PoSH> type test.txt                                                                                                     
Curiouser and curiouser!' cried Alice (she was so much surprised, that                                                  
for the moment she quite forgot how to speak good English); 'now I'm                                                    
opening out like the largest telescope that ever was! Good-bye, feet!'                                                  
(for when she looked down at her feet, they seemed to be almost out of                                                  
sight, they were getting so far off).                                                                                   
PoSH>                                                                                                                   
PoSH> (gc test.txt | out-string).IndexOf('telescope')                                                                   
173                                                                                                                     
PoSH>                                                                                                                   
PoSH> (gc test.txt | out-string).substring(173)                                                                         
telescope that ever was! Good-bye, feet!'                                                                               
(for when she looked down at her feet, they seemed to be almost out of                                                  
sight, they were getting so far off).                                                                                   
                                                                                                                        
                                                                                                                        
PoSH>                                                                                                                   
PoSH> Get-Content test.txt | out-string |% {$_.substring($_.IndexOf('telescope'))}                                      
telescope that ever was! Good-bye, feet!'                                                                               
(for when she looked down at her feet, they seemed to be almost out of                                                  
sight, they were getting so far off).                                                                                   
                                                                                                                        
                                                                                                                        
PoSH>                                                                                                                   
PoSH> [io.file]::ReadAllText('c:\scripts\test.txt') |% {$_.substring($_.IndexOf('telescope'))}                          
telescope that ever was! Good-bye, feet!'                                                                               
(for when she looked down at her feet, they seemed to be almost out of                                                  
sight, they were getting so far off).                                                                                   
PoSH>                                                                                                                   
PoSH> $Contents = [io.file]::ReadAllText('c:\scripts\test.txt')                                                         
PoSH> $Index = $Contents.IndexOf('telescope')                                                                           
PoSH> $Contents.Substring($index)                                                                                       
telescope that ever was! Good-bye, feet!'                                                                               
(for when she looked down at her feet, they seemed to be almost out of                                                  
sight, they were getting so far off).                                                                                   
PoSH>                                                                                

 again much simpler and shorter as the original VbScript.

Yes, I know that another method is used here and using this way would also be posible in VbScript making also the Vbscript version a whole bit shorter, but still I think it show show easy it is to this kind if text processing interactively in PowerShell. 

Enjoy,

Greetings /\/\o\/\/

Published Sunday, April 01, 2007 9:15 AM by admin
Filed under: , ,

Comments

# re: Hey, powerShell Guy !How Can I Locate a Word in a Text File and Then Echo Back That Word and Any Remaining Text?

Is it possible to use get-content to output the row number where a specific string exists in a text file.  I want to use that row number to extract a range of text from a text file with a varying length body but constant headers.

Tuesday, June 12, 2007 6:34 PM by northbergenguy
Anonymous comments are disabled