Solved

remove string in script sql

Posted on 2016-08-12
10
69 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
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…

707 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

18 Experts available now in Live!

Get 1:1 Help Now