Dump Contents of File for -Replace

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 14 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
homeshark
Posts: 7
Last visit: Mon Jun 15, 2009 5:53 am

Dump Contents of File for -Replace

Post by homeshark »

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.

Code: Select all

clear-Host
$file = gci "D:Documents and SettingsOwnerDesktoptemp*.*"
$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

Code: Select all

clear-Host
$file = gci "D:Documents and SettingsmaintcdDesktoptemp*.*"
$file
foreach ($str in $file) 
{
$cont = get-content -path $str
$cont | foreach-object {$_ -replace get-content -path "D:Documents and SettingsOwnerDesktopreplace2.txt} | set-content $str
}
write-host "After `n"
$file
Ideas? Thank you for looking over at this.

-Chase homeshark2009-06-12 11:09:26
User avatar
homeshark
Posts: 7
Last visit: Mon Jun 15, 2009 5:53 am

Dump Contents of File for -Replace

Post by homeshark »

Sorry I posted a wrong version of it, basically I have a text file that contains the "find", "replace" information for the -replace. This is an example of the contents inside the file

Code: Select all

"[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.", "a.b.c."
"chase","removed"

So instead of me having to write a bunch of these:

Code: Select all

$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.

Does this clear things up?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Dump Contents of File for -Replace

Post by jvierra »

Code: Select all

 
	
# Loop through a bunch of file and apply a lot of things
#
$filemask="c:temp*.txt"
$files = Get-ChildItem $filemask
foreach( $file in $files ){ 
    $repl = Get-Content "replacements.txt"
    $repl | %{ 
        $s = $_.Split(",")
        $cont = $cont | 
            foreach{
                $file -replace $s[0], $s[1]
            }
    }
    Set-Content $file $cont        
}
	
User avatar
homeshark
Posts: 7
Last visit: Mon Jun 15, 2009 5:53 am

Dump Contents of File for -Replace

Post by homeshark »

Right now it is replacing all the contents inside its file with its file name. I'll see what I can move around in it to get it working. Thx for your effort!
User avatar
homeshark
Posts: 7
Last visit: Mon Jun 15, 2009 5:53 am

Dump Contents of File for -Replace

Post by homeshark »

It seems to be replacing each possible entry in the txt files with the full path of the filename.

Code: Select all

D:Documents and SettingsownerDesktoptemptest1.txt
D:Documents and SettingsownerDesktoptemptest1.txt
D:Documents and SettingsownerDesktoptemptest1.txt
D:Documents and SettingsownerDesktoptemptest1.txt
D:Documents and SettingsownerDesktoptemptest1.txt
D:Documents and SettingsownerDesktoptemptest1.txt
D:Documents and SettingsownerDesktoptemptest1.txt
D:Documents and SettingsownerDesktoptemptest1.txt
D:Documents and SettingsownerDesktoptemptest1.txt
D:Documents and SettingsownerDesktoptemptest1.txt
D:Documents and SettingsownerDesktoptemptest1.txt

The only portion of your code I replaced was the path to test files

Code: Select all

# Loop through a bunch of file and apply a lot of things
#
$repl = Get-Content "D:Documents and SettingsownerDesktopreplacements.txt"
$filemask="D:Documents and SettingsownerDesktoptemp*.txt"
$files = Get-ChildItem $filemask
foreach( $file in $files ){
    $cont = Get-Content $file
    $repl | %{
         $s = $_.Split(",")
        $cont = $cont |
             foreach{
                $file -replace $s[0], $s[1]
            }
    }
    Set-Content $file $cont
}

Contents of the replace file

Code: Select all

"[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.", "a.b.c.""chase","removed"

contents of the test file

Code: Select all

what are chase and his friends
what are chasee and his friends
what are <chase> and his friends
what are *chase* and his friends
what are .*chase.* and his friends
what are 12chase12 and his friends
what are 100.2.1.5 and his friends
what are 128.55.315.1 and his friends
what are 255.255.255.255 and his friends
what are 234234234234234234234 and his friends
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Dump Contents of File for -Replace

Post by jvierra »

Now your input file has quotation marks in it. That changes everything as they will screw up RegEx.

Are you sure you don't want to use a CSV file?

YOu have also expanded the set of rules.

I suggest that you use th e technique and try to work out what you are trying to do. Post back with a specific question and we will help.

The RegEx loop works with a file that has NO quotation marks around the strings and NO commas in the text except to separate teh elements. YOu can easily change that to a pipe | or other delimiter if it conflicts with patterns.

User avatar
homeshark
Posts: 7
Last visit: Mon Jun 15, 2009 5:53 am

Dump Contents of File for -Replace

Post by homeshark »

My input file I believe always had the quotation files in previous post. I will do anything that is necessary to get this to work. I am VERY flexible. If you say a CSV file will work better then lets switch to a CSV file.

When you say that I had expanded the rules and messed with the regex functionality did you mean with this input file?

Code: Select all

	"[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.", "a.b.c."
That code was to remove any IP addresses and leave only the last octet. This can be removed if need be if this is what was interfering.

So my specific question is, what would I need to change to your specifications to get this to work with an input file?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Dump Contents of File for -Replace

Post by jvierra »

Sorry - you are right. The test files did have quotes. The extra comma is also there so I dind't look hard enough.

Here is a version that works using your latest test files. I changed it to use a ppe chartacter. it must be unzipped to c:testmatch and run from there.

I did have one error in the version I posted to you because I had not replaced the $file with the $_ when converting it to run multiple files.

uploads/2491/testmatch.zip

If you look closely at how the code is structured and try to understtand it you will see that it is pertty simple to modify and control.

jvierra2009-06-15 10:41:49
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Dump Contents of File for -Replace

Post by jvierra »

When using RegEx string it can become necessary to create a custom format. This cannot be established until you know the exact strings as an delimiters that are used for the file format need to be "escaped" in the file fields.

Here is where a database can help or even a speadsheet.

Usually we don't need teh pipe char in Regex but, if you need to "OR" a set of selections, you will have to change from using the pipe character.

If you can guarantee never using a quote character then we could use a CSV file. That woiuld require always using the octtal, hex or other non character form of a quote in expressions.

Only you can determine these things. If this is for a larger long term project I would suggest a database or spreadsheet as it will correctly guard against these issues. If thi is just for fun or learning then we should skip that at this point. YOu will see how to accomplish those things when you become more proficient with scripting.


User avatar
homeshark
Posts: 7
Last visit: Mon Jun 15, 2009 5:53 am

Dump Contents of File for -Replace

Post by homeshark »

Sadly this will be a long term thing due to new company policies. We will have text between quotes that will need to be removed but I will NEVER have a need to remove quotations. I just want to do whatever is easiest. It could at some point be a lengthy "master keyword" file.
This topic is 14 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked