How to display text after pattern in vbscript?

末鹿安然 提交于 2019-12-25 04:25:25

问题


I have a file with the following text as a result of my other vbscript:

              "Name": "stopped"
            "LaunchTime": "2015-02-13<some-text>", 
            "InstanceId": "i-<something>", 
                "Name": "stopped"
            "LaunchTime": "2015-02-13T17:24:11.000Z", 
            "InstanceId": "i-<something>", 
                "Name": "stopped"
            "LaunchTime": "2015-02-12<some-text>", 
            "InstanceId": "i-<something>", 

I want to use 4 characters as delimiter -> ": " (which is 4 characters) and display everything at the right side of it, so the result will look like this:

"stopped"
"2015-02-13<some-text>", 
"i-<something>", 

"stopped"
"2015-02-13T17:24:11.000Z", 
"i-<something>", 

 "stopped"
"2015-02-12<some-text>", 
"i-<something>", 

How can I accomplish such thing using vbscript? It seems that in bash, perl, awk and even linux command line things like that are so simple. Is there an easy way to do it? I was looking into split but I can't figure it out how to do it.

Thank you


回答1:


The simplest way would be to Split the lines at : (a colon followed by a space) and display the second field (index 1) of the resulting array:

Set fso = CreateObject("Scripting.FileSystemObject")

Set f = fso.OpenTextFile("C:\path\to\your.txt")
Do Until f.AtEndOfStream
  line = f.ReadLine
  If InStr(line, ": ") > 0 Then WScript.Echo Split(line, ": ", 2)(1)
Loop
f.Close



回答2:


Or, if you want a RegExp, extract the data after the ": ". In code:

Option Explicit

Dim s : s = Replace(Join(Array( _
 "            'Name': 'stopped'                       ", _
 "          'LaunchTime': '2015-02-13<some-text>',    ", _
 "          'InstanceId': 'i-<something>',            ", _
 "              'Name': 'stopped'                     ", _
 "          'LaunchTime': '2015-02-13T17:24:11.000Z', ", _
 "          'InstanceId': 'i-<something>',            ", _
 "              'Name': 'stopped'                     ", _
 "          'LaunchTime': '2015-02-12<some-text>',    ", _
 "          'InstanceId': 'i-<something>',            "  _
), vbCrLf), "'", """")
WScript.Echo s
WScript.Echo "-----------"
Dim r : Set r = New RegExp
r.Global  = True
r.Pattern = ": (""[^""]+"",?)"
Dim ms : Set ms = r.Execute(s)
Dim i
For i = 1 To ms.Count
    WScript.Echo ms(i - 1).SubMatches(0)
    If 0 = i Mod 3 Then WScript.Echo
Next

output:

cscript 28751963.vbs
            "Name": "stopped"
          "LaunchTime": "2015-02-13<some-text>",
          "InstanceId": "i-<something>",
              "Name": "stopped"
          "LaunchTime": "2015-02-13T17:24:11.000Z",
          "InstanceId": "i-<something>",
              "Name": "stopped"
          "LaunchTime": "2015-02-12<some-text>",
          "InstanceId": "i-<something>",
-----------
"stopped"
"2015-02-13<some-text>",
"i-<something>",

"stopped"
"2015-02-13T17:24:11.000Z",
"i-<something>",

"stopped"
"2015-02-12<some-text>",
"i-<something>",


来源:https://stackoverflow.com/questions/28751963/how-to-display-text-after-pattern-in-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!