2008 Scripting Games - Advanced Event 9
Event 9: You’re Twisting My Words
$content = [System.IO.File]::ReadAllText("C:\Scripts\alice.txt") $reversed = $content.split() | foreach {"$($_[($_.length-1)..0])" -replace " " } [string]::Join(" ",$reversed)
resuoiruC' dna '!resuoiruc deirc ecilA ehs( saw os hcum ,desirprus taht rof eht tnemom ehs etiuq togrof woh ot kaeps doog ;)hsilgnE won' m'I gninepo tuo ekil eht tsegral epocselet taht reve !saw ,eyb-dooG '!teef rof( nehw ehs dekool nwod ta reh ,teef yeht demees ot eb tsomla tuo fo ,thgis yeht erew gnitteg os raf .)ffo ,hO' ym roop elttil ,teef I rednow ohw lliw tup no ruoy seohs dna sgnikcots rof uoy ,won ?sraed m'I erus _I_ t'nahs eb !elba I llahs eb a taerg lae d oot raf ffo ot elbuort flesym tuoba :uoy uoy tsum eganam eht tseb yaw uoy ;nac tub-- I tsum eb dnik ot ',meht thguoht ,ecilA ro' spahrep yeht t'now klaw eht yaw I tnaw ot !og teL em :ees ll'I evig meht a wen riap fo stoob yreve '.samtsirhC
4 comments:
is $FileIO=[system.io.file]::readalltext("c:\powershell\psinfo.txt") is different than
$fileinfo=[string]get-content psinfo.txt
if NO then when i run $fileio.split("\") why do i get different format of text.
Both are of type [string] but the content structure is different.
[system.io.file]::readalltext retains the file's formating (line breaks etc) while the get-content string cast returns the file content as one line.
When you cast a collection (e.g get-content) to a string PowerShell uses the $OFS automatic variable value to delimit the collection items. The default value is the space character.
You can read more about $OFS here:
http://blogs.msdn.com/powershell/archive/2006/07/15/What-is-OFS.aspx
And if you need to force get-content to retain line breks then set $OFS with a new line:
$OFS = "`n"
$fileinfo=[string] (get-content psinfo.txt)
Don't forget to reset it back to default when you're done :)
$OFS = " "
-Shay
Big thank you for so clearly explaining the concept. I'll keep this in mind. Also I like few of examples in scripting games and this was quite special one for me. I will create few examples out of it for sure to learn and understand more. Please to see that you put my blog under your blogroll. I no longer blog there, i shifted to http:\\techstarts.wordpress.com
You're welcome :) I've also updated your blog title & url.
-Shay
Post a Comment