Solved

replace block text

Posted on 2016-07-31
4
48 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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 antispam), the admini…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

628 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