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\/\/