Re: Replace lines in a text file?

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



This is great Bruce what if i have other characters in the way e.g. i wan to
replace

<add key="Prime.Global.WebSuperVisorRole" value="webfarm\ACCESS_SUPS"/>
<add key="Prime.Global.WebSuperVisorRole" value="rex\ACCESS_SUPS"/>

Craig

"Bruce Payette [MSFT]" wrote:

> If you're using msh here's a quick way to do what Crash suggested. Say you
> want to turn every instance of "href" into "HREF" in a file named
> "file.htm". The following script will do that:
>
> $data = (get-content file.htm) -replace "href","HREF"; $data >
> file.htm
>
> This example loads the entire script into memory at once so you probably
> don't want to use it process huge scripts. The other way to do this is to
> use a temp file. That solution would look like:
>
> get-content file.htm | foreach { $_ -replace "href","HREF" } > temp.htm
> move-item -force temp.htm file.htm
>
> The first line writes the transformed file into temp.htm and the second line
> renames temp.htm to file.htm, overwriting the existing file.
>
> --
> Bruce Payette [MSFT]
> Microsoft Command Shell Development
> Microsoft Corporation
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
> ""Crash" Dummy" <dvader@xxxxxxxxxxxxx> wrote in message
> news:e%23rLl7v7FHA.3976@xxxxxxxxxxxxxxxxxxxxxxx
> >> I am needing to read a text file, more specifically a certain line (let's
> >> just say line 4) of the text file, match it against a pattern using an IF
> >> THEN statement, and if it does not match the pattern I need to replace
> >> that
> >> line with a set string. I am familiar with opening and closing, read and
> >> writing to text files, but I cannot find a solution for line replacement.
> >
> > That's because there isn't one. You can't edit files in situ.
> >
> >> I also know that you cannot simultaneously write to a file while it is
> >> open
> >> for a read. Is there a solution to this?
> >
> > The only solution to this is either to read the whole file, close it, and
> > reopen
> > it for writing, or create a new file for writing.
> >
> > For your particular situation, there are a couple of ways to go at it,
> > depending
> > on how big the file you want to edit is. If it is small, you can load the
> > whole
> > file, edit the line in question, then write it back to the file. If the
> > file is
> > large, you read it one line at a time, edit the line or lines you want,
> > and
> > write back to a temporary file. When you are done, delete the original and
> > rename the temp to the original name.
> > --
> > Crash
> >
> >
> >
>
>
>
.



Relevant Pages

  • Re: Replace lines in a text file?
    ... > I am needing to read a text file, more specifically a certain line (let's ... > just say line 4) of the text file, match it against a pattern using an IF ... > writing to text files, but I cannot find a solution for line replacement. ... You can't edit files in situ. ...
    (microsoft.public.windows.server.scripting)
  • Re: Marin IJ Concord Review/Interview
    ... Except that I'm not writing about upper middle class guys with options. ... My wife had enough of Bruce ... Trixie & Chad can afford to go multiple nights, but Debbie & Joey got bills ...
    (rec.music.artists.springsteen)
  • Re: boot.ini
    ... Bruce Lawrence wrote: ... > Can someone ell me how to access msconfig. ... How can I edit them? ... Windows Explorer, Tools> Folder Options> View, select "Show hidden ...
    (microsoft.public.win2000.general)
  • Re: boot.ini
    ... Bruce Lawrence wrote: ... > Can someone ell me how to access msconfig. ... How can I edit them? ... Windows Explorer, Tools> Folder Options> View, select "Show hidden ...
    (microsoft.public.win2000.new_user)
  • Re: Cant open links
    ... > Aru you choosing Advanced and then Edit? ... > regsvr32 actxprxy.dll ... > Bruce Hagen ... >>> is probably in the Win98 (i386 for WinXP) folder of your installation ...
    (microsoft.public.windows.inetexplorer.ie6_outlookexpress)