change line in text file

Author Message
dabing

  • Total Posts : 88
  • Scores: 0
  • Reward points : 0
  • Joined: 5/8/2009
  • Status: offline
change line in text file Thursday, May 27, 2010 10:54 AM (permalink)
0
Can someone help me on changing a whole line in a text file? For example if I have a line that says "site status: 0" on line 1. I want to change the whole line to say "site status: 1"
 
#1
    TomRiddle

    • Total Posts : 620
    • Scores: 12
    • Reward points : 0
    • Joined: 2/7/2008
    • Location: Australia
    • Status: offline
    Re:change line in text file Tuesday, June 01, 2010 1:04 AM (permalink)
    0
    Ok here is my first powershell script written by me by scratch.

    $count=0
    $Status=(get-content c:\sitestatus.txt )
    clear-content c:\sitestatus.txt
    foreach ($i in $Status) {
    $count++
    if ($count -eq 1) {
    $out=$i -replace "site status: 0", "site status: 1"
    }
    else
    {
    $out=$i
    }
    $out | add-content  c:\sitestatus.txt
    }


    I am sure it can be refined and I am also sure it can be done better but this is the best I can muster at this stage in my learning.
    -join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
     
    #2
      ebgreen

      • Total Posts : 8227
      • Scores: 98
      • Reward points : 0
      • Joined: 7/12/2005
      • Status: offline
      Re:change line in text file Tuesday, June 01, 2010 1:13 AM (permalink)
      0
      I like it. If I understand the OP though the number of the line is unknown. So I think I would do it like this:

      $out = @()
      foreach ($line in $(Get-Content c:\status.txt)){
        if ($line -eq 'site status: 0'){
          $line = 'site status: 1'
        }
        $out += $line
      }
      $out | Out-File -FilePath c:\status.txt
      "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
      Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
      http://www.visualbasicscript.com/m_47117/tm.htm
       
      #3
        TomRiddle

        • Total Posts : 620
        • Scores: 12
        • Reward points : 0
        • Joined: 2/7/2008
        • Location: Australia
        • Status: offline
        Re:change line in text file Tuesday, June 01, 2010 10:54 AM (permalink)
        0
        Thanks Ebgreen, I will study this, looks like outfile might be better than add-content in this case and I will have to work out what @() does

        Yes my version only replaces line1 of the file
        -join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
         
        #4
          ebgreen

          • Total Posts : 8227
          • Scores: 98
          • Reward points : 0
          • Joined: 7/12/2005
          • Status: offline
          Re:change line in text file Tuesday, June 01, 2010 11:30 AM (permalink)
          0
          @() creates an empty array. Out-File or Add-Content either one work. The difference is one writes one time and the other has to write for every line.
          "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
          Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
          http://www.visualbasicscript.com/m_47117/tm.htm
           
          #5
            TomRiddle

            • Total Posts : 620
            • Scores: 12
            • Reward points : 0
            • Joined: 2/7/2008
            • Location: Australia
            • Status: offline
            Re:change line in text file Tuesday, June 01, 2010 11:50 AM (permalink)
            0
            Ah, thanks, I get it now. I did read that, you saved me flipping through my book :)
            -join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
             
            #6
              TomRiddle

              • Total Posts : 620
              • Scores: 12
              • Reward points : 0
              • Joined: 2/7/2008
              • Location: Australia
              • Status: offline
              Re:change line in text file Friday, April 22, 2011 5:26 PM (permalink)
              0
              I came back to this question for nostalgic reasons. (It being my first PowerShell script and all)
              This how I would do it now utilising the pipeline.
               
              $log = "C:\status.txt"
              (Get-Content $log) | foreach{$_.replace("site status: 0","site status: 1")} | Set-Content $log
              Invoke-Item $log
               
              The thing about PowerShell that really trumps vbscript is that you can knock out a simple script like this from the top of your head and not have to refer to your little code repository of snipits to read write files etc. Also what I have written there is long hand. This is truly a one liner...
               
              (GC "C:\status.txt") | %{$_.replace("site status: 0","site status: 1")} | SC "C:\status.txt"
               
              Notice how the get-content bit is in brackets? That is so the process of reading the file is complete before moving on. Now we can save to the same file we are reading from. Also notice how you can bash it out using alias names.
              <message edited by TomRiddle on Friday, April 22, 2011 5:29 PM>
              -join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
               
              #7

                Online Bookmarks Sharing: Share/Bookmark

                Jump to:

                Current active users

                There are 0 members and 1 guests.

                Icon Legend and Permission

                • New Messages
                • No New Messages
                • Hot Topic w/ New Messages
                • Hot Topic w/o New Messages
                • Locked w/ New Messages
                • Locked w/o New Messages
                • Read Message
                • Post New Thread
                • Reply to message
                • Post New Poll
                • Submit Vote
                • Post reward post
                • Delete my own posts
                • Delete my own threads
                • Rate post

                2000-2012 ASPPlayground.NET Forum Version 3.9