Announcement

Collapse
No announcement yet.

Powershell script to dump contents of file

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Powershell script to dump contents of file

    I almost have this completed... i'm sure... just my syntax is wrong?

    This is how it is now, working, which will replace 'chase' with 'REMOVED' however I want to have a text file with the "WHAT TO LOOK FOR", "REPLACE WITH" so itll look for and replace multiple items.

    clear-Host
    $file = gci "D:\Documents and Settings\Owner\Desktop\temp\*.*"
    $file
    foreach ($str in $file)
    {
    $cont = get-content -path $str
    $cont | foreach {$_ -replace "chase", "REMOVED"} | set-content $str
    $cont = get-content -path $str
    $cont | foreach {$_ -replace "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.", "a.b.c."} | set-content $str
    }
    write-host "After `n"
    $file
    So I am trying something like this but I know it is wrong but I hope you get my idea

    clear-Host
    $file = gci "D:\Documents and Settings\maintcd\Desktop\temp\*.*"
    $file
    foreach ($str in $file)
    {
    $cont = get-content -path $str
    $cont | foreach-object {$_ -replace get-content -path "D:\Documents and Settings\Owner\Desktop\replace2.txt} | set-content $str
    }
    write-host "After `n"
    $file


    So instead of me having to write a bunch of these:
    $cont = get-content -path $str$cont | foreach {$_ -replace "chase", "REMOVED"} | set-content $str
    $cont = get-content -path $str$cont | foreach {$_ -replace "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.", "a.b.c."} | set-content $str

    I can just write it once and have it look in a text file for what to find and replace.
    Ideas?

    Thank you for looking over at this.

    -Chase
    Last edited by homeshark; 12th June 2009, 19:10.

  • #2
    Re: Powershell script to dump contents of file

    what is your input file look like, and what is your expected output?

    Comment


    • #3
      Re: Powershell script to dump contents of file

      Hello!

      Our input file simply contains what is required for the -replace

      EXAMPLE:
      "chase", "REMOVED"
      "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.", "a.b.c."


      My expected output is that it will consider each line in the text file and paste it in this format

      $cont = get-content -path $str$cont | foreach {$_ -replace [1], [2]} | set-content $str

      where [1] and [2] are the 2 seperate values that are seperated by a ","

      If need be it can also just paste the whole line
      $cont = get-content -path $str$cont | foreach {$_ -replace [1]} | set-content $str

      Comment


      • #4
        Re: Powershell script to dump contents of file

        Originally posted by homeshark View Post
        My expected output is that it will consider each line in the text file and paste it in this format
        Unless I overlooked something, this is quite easy to achieve. Just type each search/replace item into separate line. Read its contents and iterate through the array with a step of two:

        Code:
        $tokens = get-content tokens.txt
        $file = get-content sourcefile.txt
        
        foreach($row in $file) {
        
            $somefile = get-content $row
        
            for ($i=0; $i -lt $tokens.length; $i+=2) {
                $valToSearch = $tokens[$i]
                $valToReplace = $tokens[$($i+1)]
                
                # Search and replace here
                $newItem = ...
            }
        }
        -vP

        Comment


        • #5
          Re: Powershell script to dump contents of file

          What would my input file have to look like?
          chase
          removed

          if I wanted to replace the word chase with the word removed

          Also what if I wanted to do a regex thing so like remove anything that matches the wildcard cha*

          I ran your script and was getting an error message I think with the script itself.

          Code:
          The term '...' is not recognized as a cmdlet, function, operable program, or sc
          ript file. Verify the term and try again.
          At line:7 char:23
          +         $newItem = ...
           <<<<     }
          Get-Content : Cannot find path 'D:\Documents and Settings\owner\Desktop\what
          are 255.255.255.255 and his friends' because it does not exist.
          At line:2 char:28
          +     $somefile = get-content  <<<< $row
          The term '...' is not recognized as a cmdlet, function, operable program, or sc
          ript file. Verify the term and try again.
          At line:7 char:23
          +         $newItem = ...
           <<<<     }
          The term '...' is not recognized as a cmdlet, function, operable program, or sc
          ript file. Verify the term and try again.
          At line:7 char:23
          +         $newItem = ...
           <<<<     }

          Comment


          • #6
            Re: Powershell script to dump contents of file

            If you don't mind using a tool that does string processing easily, here's a script done in gawk(for windows)
            Code:
            FNR==NR{ a[$1]=$2;  next }
            {
             for ( i in a){
              if ( $0 ~ i){
                gsub(i , a[i] )    
              }  
             } 
            }
            {print}
            save the code as myscript.awk and on command line
            output
            Code:
            C:\test>more replace.txt
            old new
            one two
            
            C:\test>more file.txt
            this is line one
            this is line two
            this line has old
            this line don't have new
            
            C:\test>gawk -f test.bat replace.txt  file.txt
            this is line two
            this is line two
            this line has new
            this line don't have new
            replace.txt contains your words to replace. column 1 is the word TO BE replaced, column 2 is the word to replace.

            Comment

            Working...
            X