?
Solved

replace block text

Posted on 2016-07-31
4
Medium Priority
?
52 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 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

719 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