从文本文件中提取用户名

时间:2015-08-05 04:39:56

标签: vbscript qtp

我想使用VBScript在下面的文本文件中找到所有用户名。我们每天都会收到不同用户名的文件。每个文本文件中可能有多个用户名。我想将这些用户名提取为以下格式:

sutedan / chavaji / samdave

DATA.TXT:

application;account;entitlement;owner;action;reason;request_no;creation_date;review_name;sent_to;entitlement_description
36475-5;sutedan;gmrsk_TC_BIZ_CRE|gmrsk_TC_BIZ_EM|revoke account;Off-boarding;2d71c99-1688-4d09-b132-9029bf681084< ;29.07.2015 21:44:47;revoke account;
"For the New business line CRE ""|Emerging Markets Client business profile""|Emerging Markets Prop business profile""|Global Equity Derivatives Client business profile""
|Global Equity Derivatives Prop business profile""|Fixed Income Client business profile""|Global Credit Trading Client business profile""|Prime Brokerage business profile""|
PPSN Client business profile""|Global Rates Client business profile""|Global Rates Deutsche Securities Inc. business profile""|Global Rates Prop business profile""
|RMBS Client business profile""|RMBS Prop business profile""|Valuations Services Group business""|Tradecapture: Region: Frankfurt""|TradeCapture rights for the Sydney Region
 ""|Read only profile for GT support staff wishing to view trades only for a particular business/region combination in order to remediate systemic or specific trade problems"
application;account;entitlement;owner;action;reason;request_no;creation_date;review_name;sent_to;entitlement_description
36475-5;chavaji;gmrsk_TC_BIZ_CRE|gmrsk_TC_BIZ_EM|revoke account;Off-boarding;2d71c99-1688-4d09-b132-9010bf681084< ;29.07.2015 21:44:47;revoke account;
"For the New business line CRE ""|Emerging Markets Client business profile""|Emerging Markets Prop business profile""|Global Equity Derivatives Client business profile""
|Global Equity Derivatives Prop business profile""|Fixed Income Client business profile""|Global Credit Trading Client business profile""|Prime Brokerage business profile""|
PPSN Client business profile""|Global Rates Client business profile""|Global Rates Deutsche Securities Inc. business profile""|Global Rates Prop business profile""
|RMBS Client business profile""|RMBS Prop business profile""|Valuations Services Group business""|Tradecapture: Region: Frankfurt""|TradeCapture rights for the Sydney Region
 ""|Read only profile for GT support staff wishing to view trades only for a particular business/region combination in order to remediate systemic or specific trade problems"
application;account;entitlement;owner;action;reason;request_no;creation_date;review_name;sent_to;entitlement_description
36475-2;samdave;gmrsk_TC_BIZ_CRE|gmrsk_TC_BIZ_EM|revoke account;Off-boarding;2d71c99-1688-4d09-b132-9029bf691084< ;29.07.2015 21:44:47;revoke account;
"For the New business line CRE ""|Emerging Markets Client business profile""|Emerging Markets Prop business profile""|Global Equity Derivatives Client business profile""
|Global Equity Derivatives Prop business profile""|Fixed Income Client business profile""|Global Credit Trading Client business profile""|Prime Brokerage business profile""|
PPSN Client business profile""|Global Rates Client business profile""|Global Rates Deutsche Securities Inc. business profile""|Global Rates Prop business profile""
|RMBS Client business profile""|RMBS Prop business profile""|Valuations Services Group business""|Tradecapture: Region: Frankfurt""|TradeCapture rights for the Sydney Region
 ""|Read only profile for GT support staff wishing to view trades only for a particular business/region combination in order to remediate systemic or specific trade problems"

2 个答案:

答案 0 :(得分:1)

用户名是否总是以#####-#样式编号开头?如果是这样,您可能只是在该模式发生后匹配该字段。它与稍后发现的GUID不匹配,因为GUID中唯一可能的#####-#模式后面不会有分号。

' Using the file path from your comment...
With CreateObject("Scripting.FileSystemObject")
    strText = .OpenTextFile("C:\Users\chavaji\Documents\Data.txt").ReadAll
End With

With New RegExp
    .Pattern = "\d{5}-\d;([^;]+);"
    .Global = True
    Set mc = .Execute(strText)
End With

ReDim a(mc.Count - 1)
For i = 0 To mc.Count - 1
    a(i) = mc(i).SubMatches(0)
Next

WScript.Echo Join(a, " / ")

您可以为模式提供的信息越多越好。例如,如果下一个字段始终开始gmrsk,则可以显着改善此正则表达式。

答案 1 :(得分:0)

这似乎对我有用。我的代码假定用户名出现在每个新信息部分的相同位置。我使用字段帐户作为我的起点来引用它的来源。它至少适用于样本数据。

Option Explicit

Dim fso, fOpenFile, fReadFile, data, x, userList, dataLineStart
Set fso = CreateObject("Scripting.FileSystemObject")

Set fOpenFile = fso.OpenTextFile("C:\My\Drive\Path\Data.txt")
fReadFile = fOpenFile.ReadAll
fOpenFile.close
Set fOpenFile = Nothing

data = Split(fReadFile, ";")
userList = ""
dataLineStart = 0

for each x in data
    if x = "account" then dataLineStart = 1
    if dataLineStart = 11 then userList = userList & x & " / "
    dataLineStart = dataLineStart + 1
next

userList = left(userList, len(userList)- 3)
Wscript.Echo(userList)