Replace letter $ for ;$ in csv/text file in powershell script - powershell

I have code in PowerShell and I need to replace letter $ for ;$ or insert ;
CSV file:
"11/23/2018 17:10:08"$"https://www.google.com"
"11/23/2018 17:10:20"$"https://www.yahoo.com"
And I need file:
11/23/2018 17:10:08;$https://www.google.com
11/23/2018 17:10:20;$https://www.yahoo.com
PS: Is it possible to find the titles to URLs?
(Get-Content C:\Users\user\Desktop\test\import.txt) |
Foreach-Object {$_ -replace '"'} |
Foreach-Object {$_ -replace "$", ';$'} |
Set-Content C:\Users\user\Desktop\test\export.txt
The wrong result is:
11/23/2018 17:03:46$https://www.seznam.cz;$

You need to escape $ since it has a special meaning in a regular expression (end of text).
(Get-Content C:\Users\user\Desktop\test\import.txt) |
Foreach-Object {$_ -replace '"'}|
Foreach-Object {$_ -replace "\$", ';$'} |
Set-Content C:\Users\user\Desktop\test\export.txt

It's an one-liner:
$inputFile = 'C:\test.txt'
$outputFile = 'C:\test1.txt'
[System.IO.File]::ReadAllLines($inputFile) | % { $_.Replace( '"$"', ';$' ).Trim('"') } | Out-File $outputFile

You can use multiple -replace in one command as follows:
(Get-Content C:\Users\user\Desktop\test\import.txt) |
Foreach-Object {$_ -replace '"' -replace '$', ';$'}|
Set-Content C:\Users\user\Desktop\test\export.txt

Related

Replacing text with a defined text

I have a text file which contains the following
php_configuration=up21
sql_configuration=up22
apache_configuration=up23
java_script=down
html=down
I want to replace the up21, up22 and up23 with cat, dog and elephant and keep the remaining content of the file as it is.
But when I execute the below powershell script, it will replace the strings, but wont preserve the remaining content of the text file:
$a=Get-Content -Path C:\Users\Administrator\Desktop\Pow\loc.txt |Select-String -Pattern
"php_configuration"|ForEach-Object {$_ -Replace 'up21', 'cat'}
$b=Get-Content -Path C:\Users\Administrator\Desktop\Pow\loc.txt |Select-String -Pattern
"sql_configuration"|ForEach-Object {$_ -Replace 'up22', 'dog'}
$c=Get-Content -Path C:\Users\Administrator\Desktop\Pow\loc.txt |Select-String -Pattern
"apache_configuration"|ForEach-Object {$_ -Replace 'up23', 'elephant'}
$a, $b, $c| Set-Content -Path C:\Users\Administrator\Desktop\Pow\loc.txt
You can basically do like:
(Get-Content 'C:\Users\Administrator\Desktop\Pow\loc.txt') | Foreach-Object {
$_ -replace 'up21', 'cat' `
-replace 'up22', 'dog' `
-replace 'up23', 'elephant'
} | Set-Content 'C:\Users\Administrator\Desktop\Pow\loc.txt'
Is there a need to specify "php_configuration" etc.?
EDIT: Ok, after reading #T-Me's comment below, I have changed the answer. Not the prettiest but here you go.
(Get-Content 'C:\Users\Administrator\Desktop\Pow\loc.txt') | Foreach-Object {
if($_ | Select-string -Pattern "php_configuration"){$_ -replace 'up21', 'cat'}
elseif($_ | Select-String -Pattern "sql_configuration"){$_ -replace 'up22', 'dog'}
elseif($_ | Select-String -Pattern "apache_configuration"){$_ -replace 'up23', 'elephant'}
else {$_}
} | Set-Content 'C:\Users\Administrator\Desktop\Pow\loc.txt'

Redundant code, how can I have multiple arguments per line

This script works, I want to condense it so if I add more lines to find and replace in the file I'm not being redundant.
Get-ChildItem C:\Users\JonSa\Desktop -Filter callcounts.xml | Foreach- Object{
(Get-Content $_.FullName) |
Foreach-Object {$_ -replace "#aXXXXX.ac1.vbspbx.com", ""} |
Set-Content $_.FullName
}
Get-ChildItem C:\Users\JonSa\Desktop -Filter callcounts.xml | Foreach- Object{
(Get-Content $_.FullName) |
Foreach-Object {$_ -replace "sip:", ""} |
Set-Content $_.FullName
}
I would like to accomplish this with fewer lines that leaves room for more arguments.
With only one file, don't use Get-ChildItem and a ForEach-Object
when using the -raw -parameter, you can apply the replace on the whole file
you can also append several -replace one after the other.
for the same replacement (here none) you can use an alternation | (OR)
an empty replacement can be omitted with the -replace operator (not so with the .replace() method)
$File = 'C:\Users\JonSa\Desktop\callcounts.xml'
(Get-Content $File -raw) -replace '#aXXXXX.ac1.vbspbx.com|sip:' |
Set-Content $File

Delete line if it includes a specific string

I have a text file where it will say the computer name and current date they logged in.
04/10/2017, "PC1"
04/10/2017, "PC4"
05/10/2017, "PC3"
09/10/2017, "PC2"
I'm having issues trying to run a script that will look for any line that includes "PC2" and delete that line :
get-content "c:\file.csv" | %{if($_ -match "PC2"){$_ -replace $_, ""}} | set-content c:\file.csv
(Get-Content 'C:\File.csv') -notmatch 'PC2' | Set-Content 'C:\File1.csv'
You can also use regex
File extension is csv
Import-Csv 'C:\File.csv' -Header Logged,Computer |
where {$_.Computer -ne 'PC2'} |
Export-Csv 'C:\File.csv' -NoClobber -NoTypeInformation
(Get-Content -Path 'C:\File.csv') |
Where-Object { $_ -notlike '*PC2*' } |
Set-Content -Path 'C:\File.csv'
Here you go. This utilizes an easier-to-understand wildcard comparison operator and just filters out the lines that have the matched string.

Search for >$(bla)< and replace

I must replace a value in a file. This works for normal text with this command
(Get-Content $file) | Foreach-Object {$_ -replace "SEARCH", "REPLACE"} | Set-Content $file
But now, the search text is "$(SEARCH)" (without quotes). Backslash escaping the '$' with '`$' doesn't work:
(Get-Content $file) | Foreach-Object {$_ -replace "`$(SEARCH)", "BLA"} | Set-Content $file
Any ideas? Thank you.
The -replace operator is actually a regular expression replacement not a simple string replacement, so you've got to escape the regular expression:
(Get-Content $file) | Foreach-Object {$_ -replace '\$\(SEARCH\)', "BLA"} | Set-Content $file
Note that you can suppress string interpolation by using single quotes (') of double quotes (") around string literals, which I've done above.
Macintron,
You can try something like below :
(Get-Content $file) | Foreach-Object {$_.replace('$(SEARCH)', "BLA"} | Set-Content $file
slightly (or even more) faster
sc $file ((gc $file -Raw) -replace '\$\(search\)','BLAHH')

Using PowerShell to remove lines from a text file if it contains a string

I am trying to remove all the lines from a text file that contains a partial string using the below PowerShell code:
Get-Content C:\new\temp_*.txt | Select-String -pattern "H|159" -notmatch | Out-File C:\new\newfile.txt
The actual string is H|159|28-05-2005|508|xxx, it repeats in the file multiple times, and I am trying to match only the first part as specified above. Is that correct? Currently I am getting empty as output.
Am I missing something?
Suppose you want to write that in the same file, you can do as follows:
Set-Content -Path "C:\temp\Newtext.txt" -Value (get-content -Path "c:\Temp\Newtext.txt" | Select-String -Pattern 'H\|159' -NotMatch)
Escape the | character using a backtick
get-content c:\new\temp_*.txt | select-string -pattern 'H`|159' -notmatch | Out-File c:\new\newfile.txt
Another option for writing to the same file, building on the existing answers. Just add brackets to complete the action before the content is sent to the file.
(get-content c:\new\sameFile.txt | select-string -pattern 'H`|159' -notmatch) | Set-Content c:\new\sameFile.txt
You don't need Select-String in this case, just filter the lines out with Where-Object
Get-Content C:\new\temp_*.txt |
Where-Object { -not $_.Contains('H|159') } |
Set-Content C:\new\newfile.txt
String.Contains does a string comparison instead of a regex so you don't need to escape the pipe character, and it's also faster
The pipe character | has a special meaning in regular expressions. a|b means "match either a or b". If you want to match a literal | character, you need to escape it:
... | Select-String -Pattern 'H\|159' -NotMatch | ...
This is probably a long way around a simple problem, it does allow me to remove lines containing a number of matches. I did not have a partial match that could be used, and needed it to be done on over 1000 files.
This post did help me get to where I needed to, thank you.
$ParentPath = "C:\temp\test"
$Files = Get-ChildItem -Path $ParentPath -Recurse -Include *.txt
$Match2 = "matchtext1"
$Match2 = "matchtext2"
$Match3 = "matchtext3"
$Match4 = "matchtext4"
$Match5 = "matchtext5"
$Match6 = "matchtext6"
$Match7 = "matchtext7"
$Match8 = "matchtext8"
$Match9 = "matchtext9"
$Match10 = "matchtext10"
foreach ($File in $Files) {
$FullPath = $File | % { $_.FullName }
$OldContent = Get-Content $FullPath
$NewContent = $OldContent `
| Where-Object {$_ -notmatch $Match1} `
| Where-Object {$_ -notmatch $Match2} `
| Where-Object {$_ -notmatch $Match3} `
| Where-Object {$_ -notmatch $Match4} `
| Where-Object {$_ -notmatch $Match5} `
| Where-Object {$_ -notmatch $Match6} `
| Where-Object {$_ -notmatch $Match7} `
| Where-Object {$_ -notmatch $Match8} `
| Where-Object {$_ -notmatch $Match9} `
| Where-Object {$_ -notmatch $Match10}
Set-Content -Path $FullPath -Value $NewContent
Write-Output $File
}
If you anyone having this issue while doing what suggested by Robert Brooker-
*These files have different encodings. Left file: Unicode (UTF-8) with signature. Right file: Unicode (UTF-8) without signature. You can resolve the difference by saving the right file with the encoding Unicode (UTF-8) with signature.* with Set-Content
use -Encoding UTF8
so like this
(get-content c:\new\sameFile.txt | select-string -pattern 'H`|159' -notmatch) | Set-Content c:\new\sameFile.txt -Encoding UTF8