How to convert bash script to powershell my code is here - powershell

I want to convert my bash script into powershell, my script is here
sed -i "s/DS_PROP_KEYS :=.*/$DATASOURCE_KEY/OMS_Add_Tenant_Util.sql;"

Without writing a function this is the closest I've seen to replicating sed functionality:
(Get-Content file.txt) | Foreach-Object {$_ -replace "^SourceRegexp$", "DestinationString"} | Set-Content file.txt

Related

Batch command to remove a string pattern from input file

I'm very new to scripting.
I have a couple of files File1.txt and File2.txt. "RemPattern" is the pattern which I'm expecting to find and remove recursively from the above files.
Is it possible to remove them with the help of any windows or powershell batch command?
I have seen Get-Content can be used to remove an entire line of the matched pattern, but it doesn't fit for my case.
(Get-Content 'File1.txt') -notmatch 'RemPattern' | Set-Content 'File1.txt'
Is it required to write a batch file to achieve this or is it possible to do it by batch commands?
You can try out the -replace instead of -nomatch.
(Get-Content 'D:\File.txt') -replace 'RemPattern' | Set-Content 'D:\File.txt'
I was assuming that you wanted to recurse through a set of files and not do them by manually typing the filenames. So you can:
Get-ChildItem F:\ -Filter File*.txt | Foreach-Object{
(Get-Content $_.FullName) | Foreach-Object {$_ -replace 'RemPattern'} | Set-Content $_.FullName
}
The filter here simply checks File*.txt which in your example will do the replacement for both File1.txt and File2.txt without havign to type out each file manually per line. You can change the filter as you please.

How can i run multiple replace commands from one line of batch file

I have minimum to none knowledge of powershell :(
Hi I have two possible options to replace text from an .ini file, one is a menu-style batch, where choosing an option will execute a command.
My problem is: if I use the batch code I can only change a known resolution, because I don't know how to add multiple replace actions so they work if one fails.
The Powershell code does executes MULTIPLE replace commands, but I don't know how to edit it to use it as a batch command (powershell -command etc.)
Thank you in advance :)
Batch script:
#echo off
set ffile='resolutions.ini'
set HDReady='/resolution:1280,720'
set FullHD='/resolution:1920,1080'
set QuadHD='/resolution:2560,1440'
set UltraHD='/resolution:3840,2160'
powershell -Command "(gc %ffile%) -replace %hdready%, %fullhd% | Out-File %ffile% -encoding utf8"
Powershell script:
$original_file = 'path\resolutions.ini'
$destination_file = 'path\resolutions.ini'
(Get-Content $original_file) | Foreach-Object {
$_ -replace '/resolution:1280,720', '/resolution:1920,1080' `
-replace '/resolution:2560,1440', '/resolution:1920,1080' `
-replace '/resolution:3840,2160', '/resolution:1920,1080'
} | Set-Content $destination_file
Is one (1) line what you really want?
(Get-Content 'path\resolutions.ini') | Foreach-Object {$_ -replace '/resolution:1280,720', '/resolution:1920,1080' -replace '/resolution:2560,1440', '/resolution:1920,1080' -replace '/resolution:3840,2160', '/resolution:1920,1080'} | Set-Content 'path\resolutions.ini'
or
$original_file = 'path\resolutions.ini'; $destination_file = 'path\resolutions.ini'; (Get-Content $original_file) | Foreach-Object {$_ -replace '/resolution:1280,720', '/resolution:1920,1080' -replace '/resolution:2560,1440', '/resolution:1920,1080' -replace '/resolution:3840,2160', '/resolution:1920,1080'} | Set-Content $destination_file
You can also place your script in a file and run it as such:
powershell.exe -file My\FilePath.ps1
Here is the help section from the powershell executable:
PowerShell -Help
-File
Runs the specified script in the local scope ("dot-sourced"), so that the
functions and variables that the script creates are available in the
current session. Enter the script file path and any parameters.
File must be the last parameter in the command, because all characters
typed after the File parameter name are interpreted
as the script file path followed by the script parameters.

Extend PowerShell to search/replace and perform on all files in directory?

I have a PowerShell script that converts a CSV file to JSON. I would like to do a search/replace on the JSON file after for "NULL" to null. Also do to run this script against all CSV files in the directory. How can I extend this script to do this?
cd C:\example\data
import-csv .\chapters.csv | ConvertTo-Json -Compress | Out-File .\chapters.json
Try this:
import-csv .\chapters.csv | ConvertTo-Json -Compress |
Foreach {$_ -creplace 'NULL','null'} | Out-File .\chapters.json
The -replace operators can be used to replace text within a string.

Concatenate files using PowerShell

I am using PowerShell 3.
What is best practice for concatenating files?
file1.txt + file2.txt = file3.txt
Does PowerShell provide a facility for performing this operation directly? Or do I need each file's contents be loaded into local variables?
If all the files exist in the same directory and can be matched by a simple pattern, the following code will combine all files into one.
Get-Content .\File?.txt | Out-File .\Combined.txt
I would go this route:
Get-Content file1.txt, file2.txt | Set-Content file3.txt
Use the -Encoding parameter on Set-Content if you need something other than ASCII which is the default for Set-Content.
If you need more flexibility, you could use something like
Get-ChildItem -Recurse *.cs | ForEach-Object { Get-Content $_ } | Out-File -Path .\all.txt
Warning: Concatenation using a simple Get-Content (whether or not using -Raw flag) works for text files; Powershell is too helpful for that:
Without -Raw, it "fixes" (i.e. breaks, pun intended) line breaks, or what Powershell thinks is a line break.
With -Raw, you get a terminating line end (normally CR+LF) at the
end of each file part, which is added at the end of the pipeline. There's an option for that in newer Powershells' Set-Content.
To concatenate a binary file (that is, an arbitrary file that was split for some reason and needs to be put together again), use either this:
Get-Content -Raw file1, file2 | Set-Content -NoNewline destination
or something like this:
Get-Content file1 -Encoding Byte -Raw | Set-Content destination -Encoding Byte
Get-Content file2 -Encoding Byte -Raw | Add-Content destination -Encoding Byte
An alternative is to use the CMD shell and use
copy file1 /b + file2 /b + file3 /b + ... destinationfile
You must not overwrite any part, that is, use any of the parts as destination. The destination file must be different from any of the parts. Otherwise you're up for a surprise and must find a backup copy of the file part.
a generalization based on #Keith answer:
gc <some regex expression> | sc output
Here is an interesting example of how to make a zip-in-image file based on Powershell 7
Get-Content -AsByteStream file1.png, file2.7z | Set-Content -AsByteStream file3.png
Get-Content -AsByteStream file1.png, file2.7z | Add-Content -AsByteStream file3.png
gc file1.txt, file2.txt > output.txt
I think this is as short as it gets.
In case you would like to ensure the concatenation is done in a specific order, use the Sort-Object -Property <Some Name> argument. For example, concatenate based on the name sorting in an ascending order:
Get-ChildItem -Path ./* -Include *.txt -Exclude output.txt | Sort-Object -Property Name | ForEach-Object { Get-Content $_ } | Out-File output.txt
IMPORTANT: -Exclude and Out-File MUST contain the same values, otherwise, it will recursively keep on adding to output.txt until your disk is full.
Note that you must append a * at the end of the -Path argument because you are using -Include, as mentioned in Get-ChildItem documentation.

What is the PowerShell equivalent to this Bash command?

I'm trying to create a CLI command to have TFS check out all files that have a particular string in them. I primarily use Cygwin, but the tf command has trouble resolving the path when run within the Cygwin environment.
I figure PowerShell should be able to do the same thing, but I'm not sure what the equivalent commands to grep and xargs are.
So, what would be the equivalent PowerShell version to the following Bash command?
grep -l -r 'SomeSearchString' . | xargs -L1 tf edit
Using some UNIX aliases in PowerShell (like ls):
ls -r | select-string 'SomeSearchString' | Foreach {tf edit $_.Path}
or in a more canonical Powershell form:
Get-ChildItem -Recurse | Select-String 'SomeSearchString' |
Foreach {tf edit $_.Path}
and using PowerShell aliases:
gci -r | sls 'SomeSearchString' | %{tf edit $_.Path}
I find it easier to grok using a variable, e.g.,
PS> $files = Get-ChildItem -Recurse |
Select-String 'SomeSearchString' |
%{$_.path} |
Select -Unique
PS> tf edit $files