Solved

remove string in script sql

Posted on 2016-08-12
10
108 Views
Last Modified: 2016-08-28
Hello,

I search to remove two string like this in a script file
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[MSmerge_del*]'))
DROP TRIGGER [dbo].[MSmerge*]

Open in new window

I try the following without good result
$DIRECTORY="c:\temp"
$script=$DIRECTORY + "\" + ".Drop.Triggers.sql"
$Sql = [IO.File]::ReadAllText($script)
$Sql -replace '(?s)drop trigger \[MSmerge_.*?\r\nGO' -replace '(?s) \[MSmerge_.*?\r' , '' | Out-File $script

Open in new window


Why?

Thanks
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
  • 5
  • 3
10 Comments
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 41754592
$sql -replace  'DROP TRIGGER \[dbo\].\[MSmerge\*\]', ''


send your sample .sql file
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 41755090
Guess this is a continuation from https://www.experts-exchange.com/questions/28960354/script-smo-triggers.html ?
You want to remove the complete line, I assume. What about the create statement certainly following the drop?
0
 

Author Comment

by:bibi92
ID: 41761384
Yes, I always try without result
$DIRECTORY="c:\temp"
$script=$DIRECTORY + "\" + ".Drop.Triggers.sql"
$Sql = [IO.File]::ReadAllText($script)
$Sql -replace '(?s)drop trigger \[MSmerge_.*?\r\nGO' -replace '(?s) \[MSmerge_.*?\r' , '' | Out-File $script
Thanks
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
LVL 70

Expert Comment

by:Qlemo
ID: 41762566
I didn't get your last response. You didn't answer to my questions at all. I think you want to remove all lines written out below:
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[MSmerge_del*]'))
DROP TRIGGER [dbo].[MSmerge*]
GO
CREATE TRIGGER [dbo].[MSmerge*] /* and further text here */
/* ... */
GO

Open in new window

where the "*" means text parts to ignore when searching. Correct?
0
 

Author Comment

by:bibi92
ID: 41768597
yes thanks regards
0
 

Author Comment

by:bibi92
ID: 41771453
Following, T-SQL code :
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[MSmerge_del_2DF5CAAAAF3D4CB0B6497DE4645ABC11]'))
DROP TRIGGER [dbo].[MSmerge_del_2DF5CAAAAF3D4CB0B6497DE4645ABC11]
GO
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[MSmerge_ins_2DF5CAAAAF3D4CB0B6497DE4645ABC11]'))
DROP TRIGGER [dbo].[MSmerge_ins_2DF5CAAAAF3D4CB0B6497DE4645ABC11]
GO
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[MSmerge_upd_2DF5CAAAAF3D4CB0B6497DE4645ABC11]'))
DROP TRIGGER [dbo].[MSmerge_upd_2DF5CAAAAF3D4CB0B6497DE4645ABC11]

Open in new window


Thanks
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 41771555
This works for me:
$DIRECTORY="c:\temp"
$script=$DIRECTORY + "\" + ".Drop.Triggers.sql"
(Get-Content $script) -replace '.+MSMerge_.*', '' -join "`r`n" -replace '\r\n\r\n\r\nGO', '' | Out-File $script

Open in new window

but you might have to add or remove a \r\n combination from the pattern.
0
 

Author Comment

by:bibi92
ID: 41771659
Thanks regards
0
 

Author Closing Comment

by:bibi92
ID: 41773868
thanks
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

691 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