Solved

remove string in script sql

Posted on 2016-08-12
10
97 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 69

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
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!

 
LVL 69

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 69

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

Technology Partners: 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

Utilizing an array to gracefully append to a list of EmailAddresses
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
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…
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…

726 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