Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

replace block text

Posted on 2016-07-31
4
Medium Priority
?
59 Views
Last Modified: 2016-08-04
Hello,

In a text file, I want to replace :

AS
BEGIN

with

NOT FOR REPLICATION
AS
BEGIN

How can I do it?

Thanks

Regards
0
Comment
Question by:bibi92
  • 2
  • 2
4 Comments
 
LVL 85

Expert Comment

by:oBdA
ID: 41737044
Assuming PS 3.0 or later, because of the "Get-Content -Raw":
$Sql = Get-Content -Path 'C:\Temp\test.sql' -Raw
$Sql -replace '(?s)AS\r\nBEGIN', ("NOT FOR REPLICATION`r`nAS`r`nBEGIN") | Set-Content -Path "C:\Temp\test-NFR.sql"

Open in new window

0
 

Author Comment

by:bibi92
ID: 41737045
Thanks but we use powershell 2.0
0
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 41737048
You should seriously consider upgrading to at least PS 3.0, which adds lots of nice features and fixes errors. PS 2.0 is from 2009, PS 5.0 is now available for Windows 7 /Server 2008 R2 and later.
$Sql = [IO.File]::ReadAllText("C:\Temp\test.sql")
$Sql -replace '(?s)AS\r\nBEGIN', ("NOT FOR REPLICATION`r`nAS`r`nBEGIN") | Set-Content -Path "C:\Temp\test-NFR.sql" 

Open in new window

0
 

Author Closing Comment

by:bibi92
ID: 41742548
thanks regards
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question