Powershell 如何从文件中删除重复文本?

Powershell 如何从文件中删除重复文本?,powershell,powershell-2.0,Powershell,Powershell 2.0,我有一个包含SQL Server数据库列表的文件。我想从文件中删除4个系统数据库[master、model、msdb、tempdb]。我该怎么做 Server1 [DB1] [DB2] [DB3] [master] [model] [msdb] [tempdb] Server2 [DB1] [DB2] [DB3] [master] [model] [msdb] [tempdb] Server3 [DB1] [DB2] [DB3] [master] [model] [msdb] [tempdb]

我有一个包含SQL Server数据库列表的文件。我想从文件中删除4个系统数据库[master、model、msdb、tempdb]。我该怎么做

Server1
[DB1]
[DB2]
[DB3]
[master]
[model]
[msdb]
[tempdb]
Server2
[DB1]
[DB2]
[DB3]
[master]
[model]
[msdb]
[tempdb]
Server3
[DB1]
[DB2]
[DB3]
[master]
[model]
[msdb]
[tempdb]

如果只想删除行,请执行以下操作:

grep -v -E '^\[(master|model|msdb|tempdb)\]'
如果要删除整个块,请执行以下操作:

perl -0133 -ne 'if ($_ !~ /^(master|model|msdb|tempdb)/) { print "["; print substr($_, 0, -1) }'

如果只想删除行,请执行以下操作:

grep -v -E '^\[(master|model|msdb|tempdb)\]'
如果要删除整个块,请执行以下操作:

perl -0133 -ne 'if ($_ !~ /^(master|model|msdb|tempdb)/) { print "["; print substr($_, 0, -1) }'

枚举该文件并检查该项是否不符合regexp。这与Yiuh的grep-v示例非常相似

# dbs is here an array, but anything enumerable works.
$dbs = @("[DB1]", "[DB2]", "[DB3]", "[master]", "[model]", "[msdb]", "[tempdb]", "Server2", "[DB1]", "[DB2]", "[DB3]", "[master]", "[model]", "[msdb]", "[tempdb]", "Server3", "[DB1]", "[DB2]", "[DB3]", "[master]", "[model]", "[msdb]", "[tempdb]")

$dbs | ? {!($_ -match "\[(master)|(model)|(msdb)|(tempdb)\]")}

枚举该文件并检查该项是否不符合regexp。这与Yiuh的grep-v示例非常相似

# dbs is here an array, but anything enumerable works.
$dbs = @("[DB1]", "[DB2]", "[DB3]", "[master]", "[model]", "[msdb]", "[tempdb]", "Server2", "[DB1]", "[DB2]", "[DB3]", "[master]", "[model]", "[msdb]", "[tempdb]", "Server3", "[DB1]", "[DB2]", "[DB3]", "[master]", "[model]", "[msdb]", "[tempdb]")

$dbs | ? {!($_ -match "\[(master)|(model)|(msdb)|(tempdb)\]")}

一个选项是使用-notcontains

 $system = "[master]","[model]","[msdb]","[tempdb]"
 $new_file = @(get-content db_list.txt |
   where {$system -notcontains $_})
 $new_file | out-file db_list.txt -force

一个选项是使用-notcontains

 $system = "[master]","[model]","[msdb]","[tempdb]"
 $new_file = @(get-content db_list.txt |
   where {$system -notcontains $_})
 $new_file | out-file db_list.txt -force