使用命令行或VBScript查找Windows解决方案以获取文件并查找第二个文本块并将其保存到自己的文件中。不幸的是,我们的用户群仅使用Windows客户端,因此Linux不是一个选项。还希望没有第三方工具的原始Microsoft解决方案。
我有一个看起来像这样的文本文件:
subject=/C=US/O=thawte, Inc./OU=Certification Services Division/OU=(c) 2006 thawte, Inc. - For authorized use only/CN=thawte Primary Root CA
issuer=/C=US/O=thawte, Inc./OU=Certification Services Division/OU=(c) 2006 thawte, Inc. - For authorized use only/CN=thawte Primary Root CA
-----BEGIN CERTIFICATE-----
MIIEIDCCAwigAwIBAgIQNE7VVyDV7exJ9C/ON9srbTANBgkqhkiG9w0BAQUFADCB
qTELMAkGA1UEBhMCVVMxFTATBgNVBAoTDHRoYXd0ZSwgSW5jLjEoMCYGA1UECxMf
Q2VydGlmaWNhdGlvbiBTZXJ2aWNlcyBEaXZpc2lvbjE4MDYGA1UECxMvKGMpIDIw
MDYgdGhhd3RlLCBJbmMuIC0gRm9yIGF1dGhvcml6ZWQgdXNlIG9ubHkxHzAdBgNV
BAMTFnRoYXd0ZSBQcmltYXJ5IFJvb3QgQ0EwHhcNMDYxMTE3MDAwMDAwWhcNMzYw
-----END CERTIFICATE-----
subject=/C=US/O=Thawte, Inc./CN=Thawte SSL CA
issuer=/C=US/O=thawte, Inc./OU=Certification Services Division/OU=(c) 2006 thawte, Inc. - For authorized use only/CN=thawte Primary Root CA
-----BEGIN CERTIFICATE-----
MIIEbDCCA1SgAwIBAgIQTV8sNAiyTCDNbVB+JE3J7DANBgkqhkiG9w0BAQUFADCB
qTELMAkGA1UEBhMCVVMxFTATBgNVBAoTDHRoYXd0ZSwgSW5jLjEoMCYGA1UECxMf
Q2VydGlmaWNhdGlvbiBTZXJ2aWNlcyBEaXZpc2lvbjE4MDYGA1UECxMvKGMpIDIw
MDYgdGhhd3RlLCBJbmMuIC0gRm9yIGF1dGhvcml6ZWQgdXNlIG9ubHkxHzAdBgNV
BAMTFnRoYXd0ZSBQcmltYXJ5IFJvb3QgQ0EwHhcNMTAwMjA4MDAwMDAwWhcNMjAw
-----END CERTIFICATE-----
但是,我只对符合以下条件的第二个或最后文本块感兴趣:
开始于:-----BEGIN CERTIFICATE-----
介于两者之间的所有数据,以-----END CERTIFICATE-----
请注意标题之间的文本块总是不同,但标题将保持不变。
答案 0 :(得分:1)
使用RegExp(开始阅读here和here)非空的'所有内容'分隔符之间非贪婪> str(df$Date)
Date[1:10], format: "2013-08-17" "2014-01-19" "2014-11-06" "2016-03-06" "2013-05-17" "2016-02-21" "2016-04-28" "2015-03-14" "2015-01-27" "2012-10-26"
。演示:
[\s\S]+?
输出:
Option Explicit
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim reCut : Set reCut = New RegExp
reCut.Global = True
reCut.Pattern = "-----BEGIN CERTIFICATE-----\r\n([\s\S]+?)\r\n-----END CERTIFICATE-----"
Dim oMTS : Set oMTS = reCut.Execute(goFS.OpenTextFile("..\data\39463295.txt").ReadAll())
Dim sBlock : sBlock = oMTS(oMTS.Count - 1).SubMatches(0)
WScript.Echo ">>>" & sBlock & "<<<"
更新评论:
要包含分隔符:
cscript 39463295.vbs
>>>MIIEbDCCA1SgAwIBAgIQTV8sNAiyTCDNbVB+JE3J7DANBgkqhkiG9w0BAQUFADCB
qTELMAkGA1UEBhMCVVMxFTATBgNVBAoTDHRoYXd0ZSwgSW5jLjEoMCYGA1UECxMf
Q2VydGlmaWNhdGlvbiBTZXJ2aWNlcyBEaXZpc2lvbjE4MDYGA1UECxMvKGMpIDIw
MDYgdGhhd3RlLCBJbmMuIC0gRm9yIGF1dGhvcml6ZWQgdXNlIG9ubHkxHzAdBgNV
BAMTFnRoYXd0ZSBQcmltYXJ5IFJvb3QgQ0EwHhcNMTAwMjA4MDAwMDAwWhcNMjAw<<<
输出:
Option Explicit
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim reCut : Set reCut = New RegExp
reCut.Global = True
reCut.Pattern = "-----BEGIN CERTIFICATE-----[\s\S]+?-----END CERTIFICATE-----"
Dim oMTS : Set oMTS = reCut.Execute(goFS.OpenTextFile("..\data\39463295.txt").ReadAll())
Dim sBlock : sBlock = oMTS(oMTS.Count - 1).Value
WScript.Echo ">>>" & sBlock & "<<<"
(不再需要组/ SubMatches或EOL删除)