Solved

replace block text

Posted on 2016-07-31
4
42 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 84

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 84

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 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