过滤 .txt 文件中的字符串并在 .json 文件中格式化输出

过滤 .txt 文件中的字符串并在 .json 文件中格式化输出

我正在尝试创建一个 .bat 脚本来过滤 .txt 文件中的字符串并将输出格式化为 .json 文件。

我的 .txt 文件包含如下行:

Server: http://viphome1.com:33000/c/ 
MAC: 00:1a:79:91:a6:cf 
Vence:July 15, 2022, 11:35 pm  
MAC: 00:1a:79:92:1a:b1 
Vence:May 6, 2022, 12:35 am  

目标 .json 文件是用于配置文件的文件,按照所需布局过滤后的字符串结果如下:

[
  {
    "Name": "any",
    "Address": "http://viphome1.com:33000/c/",
    "Mac": "00:1a:79:91:a6:cf"
  },
{
    "Name": "any2",
    "Address": "http://viphome1.com:33000/c/",
    "Mac": "00:1a:79:ad:83:b1"
  }
]

我的脚本.bat:

@ECHO OFF
SETLOCAL
(
FOR /f "tokens=* delims=:," %%a IN (C:\Users\dom19\Desktop\first.txt) DO (
 ECHO(
)
)>Accounts.json

TYPE Accounts.json

我只学过 html 和 css,从来没有做过批处理文件,任何帮助我都会很感激,谢谢

答案1

尽管我对 .json 格式的布局和其他未成年人的知识不是很了解,但我创建了一个 bat,它会按照问题中告知的布局生成一些文件,并进行处理以获取格式化为预期输出的字符串(或者我理解的更少)。

  • 这是我的蝙蝠,它首先创建与您的相同的文件,然后处理/返回 json。
@echo off

setlocal enableDelayedExpansion
cd /d "%~dp0" & >.\Target.json echo[[

>.\File_01.txt ^
    (
     echo\Server: http://viphome1.com:33000/a/
     echo\MAC: 00:1a:79:91:a6:cf
     echo\Vence:July 15, 2022, 11:35 pm
     echo\MAC: 00:1a:79:92:1a:b1
     echo\Vence:May 6, 2022, 12:35 am
    )
>.\File_02.txt ^
    (
     echo\Server: http://viphome1.com:33000/b/
     echo\MAC: 0a:1a:79:91:a6:cf
     echo\Vence:July 15, 2022, 11:35 pm
     echo\MAC: 0b:1a:79:92:1a:b1
     echo\Vence:May 7, 2022, 12:35 am
    ) 
>.\File_03.txt ^
    (
     echo\Server: http://viphome1.com:33000/c/
     echo\MAC: 0c:1a:79:91:a6:cf
     echo\Vence:July 15, 2022, 11:35 pm
     echo\MAC: 0d:1a:79:92:1a:b1
     echo\Vence:May 8, 2022, 12:35 am
    ) 
>.\File_04.txt ^
    (
     echo\Server: http://viphome1.com:33000/d/
     echo\MAC: 0e:1a:79:91:a6:cf
     echo\Vence:July 15, 2022, 11:35 pm
     echo\MAC: 0f:1a:79:92:1a:b1
     echo\Vence:May 9, 2022, 12:35 am
    )

set _cnt=<nul & for %%G in (.\*.txt)do <con: >>.\Target.json (
     echo\  ^{& for /f useback^tokens^=1*delims^=^":^  %%i in (
        `findstr/v "Vence" ^< "%%~G"`)do if not "!_mac!" == "" (
             echo\  ^{
             set _mac=<nul
             echo;    !_name!
             echo;    !_address!
             echo;    "%%~i": "%%~j"
           ) else if /i "%%~i" == "Server" (
             set /a "_cnt+=1+0"
             set "_n=00000!_cnt!"
             set "_address="Address": "%%~j","
             echo;    "Name": "Any_!_n:~-6!",
             echo;    !_address!
           ) else (
             set "_mac=?"
             set /a "_cnt+=1+0"
             set "_n=00000!_cnt!"
             echo;    "%%~i": "%%~j"
             set "_name="Name": "Any_!_n:~-6!","
             echo;  ^},
           )
     echo\  ^}
   )

>>.\Target.json echo;] & endlocal | type .\Target.json


您需要多个 for 循环来执行行/字符串的格式化/操作并获取 .json 文件。

我的测试文件(名称和内容):

  • .\File_01.txt:
Server: http://viphome1.com:33000/a/
MAC: 00:1a:79:91:a6:cf
Vence:July 15, 2022, 11:35 pm
MAC: 00:1a:79:92:1a:b1
Vence:May 6, 2022, 12:35 am
  • .\File_02.txt:
Server: http://viphome1.com:33000/b/
MAC: 0a:1a:79:91:a6:cf
Vence:July 15, 2022, 11:35 pm  
MAC: 0b:1a:79:92:1a:b1
Vence:May 7, 2022, 12:35 am
  • .\File_03.txt:
Server: http://viphome1.com:33000/c/
MAC: 0c:1a:79:91:a6:cf
Vence:July 15, 2022, 11:35 pm
MAC: 0d:1a:79:92:1a:b1
Vence:May 8, 2022, 12:35 am
  • .\File_04.txt:
Server: http://viphome1.com:33000/d/
MAC: 0e:1a:79:91:a6:cf
Vence:July 15, 2022, 11:35 pm
MAC: 0f:1a:79:92:1a:b1
Vence:May 9, 2022, 12:35 am

  • target.json获取的内容
[
  {
    "Name": "Any_000001",
    "Address": "http://viphome1.com:33000/a/",
    "MAC": "00:1a:79:91:a6:cf"
  },
  {
    "Name": "Any_000002",
    "Address": "http://viphome1.com:33000/a/",
    "MAC": "00:1a:79:92:1a:b1"
  }
  { 
    "Name": "Any_000003",
    "Address": "http://viphome1.com:33000/b/",
    "MAC": "0a:1a:79:91:a6:cf"
  },
  {
    "Name": "Any_000004",
    "Address": "http://viphome1.com:33000/b/",
    "MAC": "0b:1a:79:92:1a:b1"
  }
  { 
    "Name": "Any_000005",
    "Address": "http://viphome1.com:33000/c/",
    "MAC": "0c:1a:79:91:a6:cf"
  },
  {
    "Name": "Any_000006",
    "Address": "http://viphome1.com:33000/c/",
    "MAC": "0d:1a:79:92:1a:b1"
  }
  { 
    "Name": "Any_000007",
    "Address": "http://viphome1.com:33000/d/",
    "MAC": "0e:1a:79:91:a6:cf"
  },
  {
    "Name": "Any_000008",
    "Address": "http://viphome1.com:33000/d/",
    "MAC": "0f:1a:79:92:1a:b1"
  }
] 


  • 将此代码另存为any_name.cmd并尝试在包含 .txt 文件的同一文件夹中运行
@echo off

setlocal enableDelayedExpansion
cd /d "%~dp0" & >.\Target.json echo[[

set^ _cnt=<nul& for %%G in (.\*.txt)do <con: >>.\Target.json (
     echo\  ^{& for /f useback^tokens^=1*delims^=^":^  %%i in (
        `findstr/v "Vence" ^< "%%~G"`)do if not "!_mac!" == "" (
             echo\  ^{
             set _mac=<nul
             echo;    !_name!
             echo;    !_address!
             echo;    "%%~i": "%%~j"
           ) else if /i "%%~i" == "Server" (
             set /a "_cnt+=1+0"
             set "_n=00000!_cnt!"
             set "_address="Address": "%%~j","
             echo;    "Name": "Any_!_n:~-6!",
             echo;    !_address!
           ) else (
             set "_mac=?"
             set /a "_cnt+=1+0"
             set "_n=00000!_cnt!"
             echo;    "%%~i": "%%~j"
             set "_name="Name": "Any_!_n:~-6!","
             echo;  ^},
           )
     echo\  ^}
   )

>>.\Target.json echo;] & endlocal | type .\Target.json

相关内容