![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Read a line from a text file, without loading the entire file inmemory I'm currently updating a function to generate password function create-password { $leet = @{e=3;o=0;l=1;a=4;i='!';t=7} $paddingchars = "abcdefghijklmnopqrstuvwxyz01234567890".ToCharArray () $spec = "!@#*-+$%^".ToCharArray() $rand = new-object System.Random if ($args[0]) { $word = $args[0] } else { $words = import-csv dic2.csv $word = ($words[$rand.Next(0,$words.Count)]).Word } $outputchars = $word.ToCharArray() $outputchars | foreach-object { $i=0 }{ $skip = $rand.Next(0,3); if ( $skip -ne 0 -AND $leet.ContainsKey([string]$_) ) { $outputchars[$i] = [char][string]$leet[[string]$_]}; $i+=1 } while ($outputchars.Length -lt 8) { $outputchars += $paddingchars [$rand.Next(0,$paddingchars.Length)]} $OFS = "" $originalword = $word $bef=$spec[$rand.next(0,$spec.length)] $aft=$spec[$rand.next(0,$spec.length)] $word = $bef+[string]$outputchars+$aft "Generated password: $word (based on '$originalword')" } On line 6 - $words = import-csv dic2.csv => import my dictionary and choose a random word. When dic2.csv contains 1000 or 3000, the loading time is acceptable, but unfortunately my dic2.csv contains over 170000 words, so the loading time is over 3/4 seconds which is long Because I know the size of my dic2.csv, I was wondering if it was possible to get something like this: $myword = readline ("dic2.csv",$rand.next(0,170000)) returning a random line from dic2.csv without reading everything |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Read a line from a text file, without loading the entire file in memory Unless you know the length of each line (or each line has the same length) then this will be tricky. You can always use Get-Content to stream the file line-by-line. If you really need random access take a look at the System.IO.FileStream class. It allows you to open a file and seek to any position in the file and then read bytes from there. Keep in mind that you need to know the file's encoding and explicitly translate between bytes and the file's encoding. If it is ASCII then you can use [System.Text.Encoding]::ASCII.GetString(byte[] bytes). This kind of points to a minor deficiency in Get-Content - it is missing an Offset parameter to allow you specify the offset into the file that you want to start reading from. HTH, Keith "Personne" <cpdivers@xxxxxx> wrote in message news:fce0534e-4ffd-4bd8-9486-e795860ff3fe@xxxxxx Quote: > I'm currently updating a function to generate password > > function create-password { > $leet = @{e=3;o=0;l=1;a=4;i='!';t=7} > $paddingchars = "abcdefghijklmnopqrstuvwxyz01234567890".ToCharArray > () > $spec = "!@#*-+$%^".ToCharArray() > $rand = new-object System.Random > if ($args[0]) { $word = $args[0] } else { $words = import-csv > dic2.csv > $word = ($words[$rand.Next(0,$words.Count)]).Word } > $outputchars = $word.ToCharArray() > $outputchars | foreach-object { $i=0 }{ $skip = $rand.Next(0,3); if > ( $skip -ne 0 -AND $leet.ContainsKey([string]$_) ) { $outputchars[$i] > = [char][string]$leet[[string]$_]}; $i+=1 } > while ($outputchars.Length -lt 8) { $outputchars += $paddingchars > [$rand.Next(0,$paddingchars.Length)]} > $OFS = "" > $originalword = $word > $bef=$spec[$rand.next(0,$spec.length)] > $aft=$spec[$rand.next(0,$spec.length)] > $word = $bef+[string]$outputchars+$aft > "Generated password: $word (based on '$originalword')" > } > > On line 6 - $words = import-csv dic2.csv => import my dictionary and > choose a random word. > > When dic2.csv contains 1000 or 3000, the loading time is acceptable, > but unfortunately my dic2.csv contains over 170000 words, so the > loading time is over 3/4 seconds which is long > > Because I know the size of my dic2.csv, I was wondering if it was > possible to get something like this: > > $myword = readline ("dic2.csv",$rand.next(0,170000)) > > returning a random line from dic2.csv without reading everything > |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Read a line from a text file, without loading the entire file inmemory > This kind of points to a minor deficiency in Get-Content - it is missing an Quote: > Offset parameter to allow you specify the offset into the file that you want > to start reading from. parameter Don't you think? Cheers, Stuart On 10 Dec, 23:58, "Keith Hill [MVP]" <r_keith_h...@xxxxxx_no_spam_I> wrote: Quote: > Unless you know the length of each line (or each line has the same length) > then this will be tricky. *You can always use Get-Content to stream thefile > line-by-line. *If you really need random access take a look at the > System.IO.FileStream class. It allows you to open a file and seek to any > position in the file and then read bytes from there. *Keep in mind thatyou > need to know the file's encoding and explicitly translate between bytes and > the file's encoding. *If it is ASCII then you can use > [System.Text.Encoding]::ASCII.GetString(byte[] bytes). > > This kind of points to a minor deficiency in Get-Content - it is missing an > Offset parameter to allow you specify the offset into the file that you want > to start reading from. > > HTH, > Keith > > "Personne" <cpdiv...@xxxxxx> wrote in message > > news:fce0534e-4ffd-4bd8-9486-e795860ff3fe@xxxxxx > Quote: > > I'm currently updating a function to generate password Quote: > > function create-password { > > *$leet = @{e=3;o=0;l=1;a=4;i='!';t=7} > > *$paddingchars = "abcdefghijklmnopqrstuvwxyz01234567890".ToCharArray > > () > > *$spec = "!@#*-+$%^".ToCharArray() > > *$rand = new-object System.Random > > *if ($args[0]) { $word = $args[0] } else { $words = import-csv > > dic2.csv > > *$word = ($words[$rand.Next(0,$words.Count)]).Word } > > *$outputchars = $word.ToCharArray() > > *$outputchars | foreach-object { $i=0 }{ $skip = $rand.Next(0,3);if > > ( $skip -ne 0 -AND $leet.ContainsKey([string]$_) ) { $outputchars[$i] > > = [char][string]$leet[[string]$_]}; $i+=1 } > > *while ($outputchars.Length -lt 8) { $outputchars += $paddingchars > > [$rand.Next(0,$paddingchars.Length)]} > > *$OFS = "" > > *$originalword = $word > > *$bef=$spec[$rand.next(0,$spec.length)] > > *$aft=$spec[$rand.next(0,$spec.length)] > > *$word = $bef+[string]$outputchars+$aft > > *"Generated password: $word (based on '$originalword')" > > } Quote: > > On line 6 - $words = import-csv dic2.csv => import my dictionary and > > choose a random word. Quote: > > When dic2.csv contains 1000 or 3000, the loading time is acceptable, > > but unfortunately my dic2.csv contains over 170000 words, so the > > loading time is over 3/4 seconds which is long Quote: > > Because I know the size of my dic2.csv, I was wondering if it was > > possible to get something like this: Quote: > > $myword = readline ("dic2.csv",$rand.next(0,170000)) Quote: > > returning a random line from dic2.csv without reading everything |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Read a line from a text file, without loading the entire file in memory Even more fundamental than that would be to have an -AsString or some such parameter that would spit out a file's entire contents as a single string. Obviously this would be a disaster for huge files. However I process lots of small files where I need to apply a regex across the whole file's contents (not just a single line). Having to run that through string.concat (or join-string in PSCX) kind of bites. I guess the problem with adding too many different parameters is that it could start to get kind of confusing for the user - although the -start/-stop regex does sound interesting. -- Keith "Kryten" <Kryten68@xxxxxx> wrote in message news:efa64670-e31f-491f-ac02-7a30dbd9e185@xxxxxx Quote: Quote: >> This kind of points to a minor deficiency in Get-Content - it is missing >> an >> Offset parameter to allow you specify the offset into the file that you >> want >> to start reading from. > It would be great if get-content had a -start [regex] -end [regex] > parameter > Don't you think? > > Cheers, > Stuart > > > > > > > > On 10 Dec, 23:58, "Keith Hill [MVP]" > <r_keith_h...@xxxxxx_no_spam_I> wrote: Quote: >> Unless you know the length of each line (or each line has the same >> length) >> then this will be tricky. You can always use Get-Content to stream the >> file >> line-by-line. If you really need random access take a look at the >> System.IO.FileStream class. It allows you to open a file and seek to any >> position in the file and then read bytes from there. Keep in mind that >> you >> need to know the file's encoding and explicitly translate between bytes >> and >> the file's encoding. If it is ASCII then you can use >> [System.Text.Encoding]::ASCII.GetString(byte[] bytes). >> >> This kind of points to a minor deficiency in Get-Content - it is missing >> an >> Offset parameter to allow you specify the offset into the file that you >> want >> to start reading from. >> >> HTH, >> Keith >> >> "Personne" <cpdiv...@xxxxxx> wrote in message >> >> news:fce0534e-4ffd-4bd8-9486-e795860ff3fe@xxxxxx >> Quote: >> > I'm currently updating a function to generate password Quote: >> > function create-password { >> > $leet = @{e=3;o=0;l=1;a=4;i='!';t=7} >> > $paddingchars = "abcdefghijklmnopqrstuvwxyz01234567890".ToCharArray >> > () >> > $spec = "!@#*-+$%^".ToCharArray() >> > $rand = new-object System.Random >> > if ($args[0]) { $word = $args[0] } else { $words = import-csv >> > dic2.csv >> > $word = ($words[$rand.Next(0,$words.Count)]).Word } >> > $outputchars = $word.ToCharArray() >> > $outputchars | foreach-object { $i=0 }{ $skip = $rand.Next(0,3); if >> > ( $skip -ne 0 -AND $leet.ContainsKey([string]$_) ) { $outputchars[$i] >> > = [char][string]$leet[[string]$_]}; $i+=1 } >> > while ($outputchars.Length -lt 8) { $outputchars += $paddingchars >> > [$rand.Next(0,$paddingchars.Length)]} >> > $OFS = "" >> > $originalword = $word >> > $bef=$spec[$rand.next(0,$spec.length)] >> > $aft=$spec[$rand.next(0,$spec.length)] >> > $word = $bef+[string]$outputchars+$aft >> > "Generated password: $word (based on '$originalword')" >> > } Quote: >> > On line 6 - $words = import-csv dic2.csv => import my dictionary and >> > choose a random word. Quote: >> > When dic2.csv contains 1000 or 3000, the loading time is acceptable, >> > but unfortunately my dic2.csv contains over 170000 words, so the >> > loading time is over 3/4 seconds which is long Quote: >> > Because I know the size of my dic2.csv, I was wondering if it was >> > possible to get something like this: Quote: >> > $myword = readline ("dic2.csv",$rand.next(0,170000)) Quote: >> > returning a random line from dic2.csv without reading everything |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Read a line from a text file, without loading the entire file inmemory On Dec 11, 2:49*pm, "Keith Hill [MVP]" <r_keith_h...@xxxxxx_spam_I> wrote: Quote: > Even more fundamental than that would be to have an -AsString or some such > parameter that would spit out a file's entire contents as a single string.. > Obviously this would be a disaster for huge files. *However I process lots > of small files where I need to apply a regex across the whole file's > contents (not just a single line). *Having to run that through string.concat > (or join-string in PSCX) kind of bites. * I guess the problem with adding > too many different parameters is that it could start to get kind of > confusing for the user - although the -start/-stop regex does sound > interesting. > > -- > Keith > > "Kryten" <Kryte...@xxxxxx> wrote in message > > news:efa64670-e31f-491f-ac02-7a30dbd9e185@xxxxxx > Quote: Quote: > >> This kind of points to a minor deficiency in Get-Content - it is missing > >> an > >> Offset parameter to allow you specify the offset into the file that you > >> want > >> to start reading from. Quote: > > It would be great if get-content had a -start [regex] -end [regex] > > parameter > > Don't you think? Quote: > > Cheers, > > Stuart Quote: > > On 10 Dec, 23:58, "Keith Hill [MVP]" > > <r_keith_h...@xxxxxx_no_spam_I> wrote: Quote: > >> Unless you know the length of each line (or each line has the same > >> length) > >> then this will be tricky. *You can always use Get-Content to stream the > >> file > >> line-by-line. *If you really need random access take a look at the > >> System.IO.FileStream class. It allows you to open a file and seek to any > >> position in the file and then read bytes from there. *Keep in mind that > >> you > >> need to know the file's encoding and explicitly translate between bytes > >> and > >> the file's encoding. *If it is ASCII then you can use > >> [System.Text.Encoding]::ASCII.GetString(byte[] bytes). Quote: Quote: > >> This kind of points to a minor deficiency in Get-Content - it is missing > >> an > >> Offset parameter to allow you specify the offset into the file that you > >> want > >> to start reading from. Quote: Quote: > >> HTH, > >> Keith Quote: Quote: > >> "Personne" <cpdiv...@xxxxxx> wrote in message Quote: Quote: > >>news:fce0534e-4ffd-4bd8-9486-e795860ff3fe@xxxxxx Quote: Quote: > >> > I'm currently updating a function to generate password Quote: Quote: > >> > function create-password { > >> > *$leet = @{e=3;o=0;l=1;a=4;i='!';t=7} > >> > *$paddingchars = "abcdefghijklmnopqrstuvwxyz01234567890".ToCharArray > >> > () > >> > *$spec = "!@#*-+$%^".ToCharArray() > >> > *$rand = new-object System.Random > >> > *if ($args[0]) { $word = $args[0] } else { $words = import-csv > >> > dic2.csv > >> > *$word = ($words[$rand.Next(0,$words.Count)]).Word } > >> > *$outputchars = $word.ToCharArray() > >> > *$outputchars | foreach-object { $i=0 }{ $skip = $rand.Next(0,3); if > >> > ( $skip -ne 0 -AND $leet.ContainsKey([string]$_) ) { $outputchars[$i] > >> > = [char][string]$leet[[string]$_]}; $i+=1 } > >> > *while ($outputchars.Length -lt 8) { $outputchars += $paddingchars > >> > [$rand.Next(0,$paddingchars.Length)]} > >> > *$OFS = "" > >> > *$originalword = $word > >> > *$bef=$spec[$rand.next(0,$spec.length)] > >> > *$aft=$spec[$rand.next(0,$spec.length)] > >> > *$word = $bef+[string]$outputchars+$aft > >> > *"Generated password: $word (based on '$originalword')" > >> > } Quote: Quote: > >> > On line 6 - $words = import-csv dic2.csv => import my dictionaryand > >> > choose a random word. Quote: Quote: > >> > When dic2.csv contains 1000 or 3000, the loading time is acceptable, > >> > but unfortunately my dic2.csv contains over 170000 words, so the > >> > loading time is over 3/4 seconds which is long Quote: Quote: > >> > Because I know the size of my dic2.csv, I was wondering if it was > >> > possible to get something like this: Quote: Quote: > >> > $myword = readline ("dic2.csv",$rand.next(0,170000)) Quote: Quote: > >> > returning a random line from dic2.csv without reading everything function Slurp-File { param([string]$file_path) [System.IO.StreamReader]$reader = (gi $file_path).fullname return $reader.ReadToEnd() } |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Read a line from a text file, without loading the entire file in memory Quote: > > $myword = readline ("dic2.csv", > $rand.next(0,170000)) > > returning a random line from dic2.csv > without reading everything > How about Microsoft's IIS's (local or remote) data parser, Log Parser, that comes with many input engines, to do what one needs or write one's own Log Parser input and output engines (perhaps even within powershell.exe)! @" cat dog animal bird mouse horse "@ | out-file dic2.txt -encoding ascii $rand = new-object System.Random $totalCount = LogParser.exe "SELECT COUNT(*) FROM dic2.txt " ` -i textword -q on $pick = $rand.Next(0,$totalCount) $word = LogParser.exe "SELECT Text FROM dic2.txt WHERE index = $($pick + 1) " ` -i textword -q on " " "Word is $word" " " "Done!" " " Exit Returns: Word is cat Done! For more help, ask any PowerShell user or MVP or any IIS user or MVP (where Log Parser comes from, notice that there is no need to have IIS installed in order to use Log Parser within the Windows admin's automation tool, powershell.exe) or just perhaps: LogParser.exe -h Have fun data parsing within powershell.exe! |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Read a line from a text file, without loading the entire file in memory [System.IO.File]::ReadAllText($path) is easier. :-) -- Keith "tojo2000" <tojo2000@xxxxxx> wrote in message news:44b0c3c1-6da7-4ee2-a091-7b27843882a9@xxxxxx Quote: > On Dec 11, 2:49 pm, "Keith Hill [MVP]" > > In that case you should try creating a System.IO.StreamReader object: > > function Slurp-File { > param([string]$file_path) > [System.IO.StreamReader]$reader = (gi $file_path).fullname > return $reader.ReadToEnd() > } > > |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Read a line from a text file, without loading the entire file inmemory On Dec 12, 9:00*pm, "Keith Hill [MVP]" <r_keith_h...@xxxxxx_spam_I> wrote: Quote: > [System.IO.File]::ReadAllText($path) is easier. *:-) > > -- > Keith > > "tojo2000" <tojo2...@xxxxxx> wrote in message > > news:44b0c3c1-6da7-4ee2-a091-7b27843882a9@xxxxxx > Quote: > > On Dec 11, 2:49 pm, "Keith Hill [MVP]" Quote: > > In that case you should try creating a System.IO.StreamReader object: Quote: > > function Slurp-File { > > *param([string]$file_path) > > *[System.IO.StreamReader]$reader = (gi $file_path).fullname > > *return $reader.ReadToEnd() > > } I like streamreader because it has the readline() method, so that tends to be my starting point. |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| read text file - but starting at a specific point (not the very first character of the very first line) | VB Script | |||
| How to read the LAST NON-BLANK line from a text file? | VB Script | |||
| The next line in a text file | PowerShell | |||
| How do I read a text file and sort text by fixed positions? | PowerShell | |||
| new line in text file with out-file | PowerShell | |||