Re: [MSH] Applying regular expression to whole file

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




another way to achieve this is using the join method on the string object

[string]::Join("`n",(get-content old.txt))




"Bruce Payette [MSFT]" <Bruce Payette [MSFT]@discussions.microsoft.com>
wrote in message news:44FB83CE-3380-47AC-AD34-3967BE1E9F13@xxxxxxxxxxxxxxxx
> So - here's a wierd little trick using a sort-of bug in out-string. The
> out-string cmdlet takes it's input and returns the result as a single
> string
> with newlines. This let's you do something like:
>
> (get-content old.txt | out-string) -replace "foo","bar" > new.txt
>
> -bruce
>
> "Jouko Kynsijärvi" wrote:
>
>> Jack Ukleja wrote:
>> > Is there an easy way to load an entire text file as a single string
>> > into a MSH variable? Perhaps there is some MSH syntax that does
>> > exactly what I want that ive missed?
>>
>> You can concatenate all lines with:
>> $text = "$(get-content test.txt)"
>>
>> Or if you want lines separated by CR&LF instead of spaces:
>> $OFS = "`r`n"
>> $text = "$(get-content test.txt)"
>>
>> Or the framework way:
>> $text = [IO.File]::ReadAll('test.txt')
>>
>>
>>


.



Relevant Pages

  • Re: Please,help as soon as you can.
    ... It represents slightly more that a minimal string ... > This constructor creates an empty String with the given capacity. ... This includes duplicating the array of characters ... > The constructor creates a String object of the given capacity and ...
    (comp.lang.cpp)
  • Re: After deserialization program occupies about 66% more RAM
    ... by the string object. ... "interned reference" is probably a bit better. ... When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equalsmethod, then the string from the pool is returned. ...
    (comp.lang.java.programmer)
  • Please,help as soon as you can.
    ... It represents slightly more that a minimal string class. ... This constructor creates an empty String with the given capacity. ... This includes duplicating the array of characters owned ... The constructor creates a String object of the given capacity and sets its ...
    (comp.lang.cpp)
  • Re: Difference between String variable and String Class definition
    ... The first line allocates memory for the literal "java". ... It copies the string from the first place to the new place ... Are you saying that the first example does not produce a String object ...
    (comp.lang.java.programmer)
  • Re: programming concepts > specific languages
    ... >> On the second line, the value of the reference is passed along, not the ... >the string, and performs a therefore useless manipulation of it. ... Immutability of objects is, AFAIK, implemented simply by not providing ... myString *which is a reference to a String object*, ...
    (comp.programming)