Solved

remove string in script sql

Posted on 2016-08-12
10
79 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
  • 5
  • 3
10 Comments
 
LVL 18

Expert Comment

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


send your sample .sql file
0
 
LVL 68

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
 
LVL 68

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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 68

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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now