如何使用FOR批处理命令查找两个特定符号之间的所有文本? [英] How do I use the FOR batch command to find all text between two specific symbols?

查看:185
本文介绍了如何使用FOR批处理命令查找两个特定符号之间的所有文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



输入文件有这样的文本行:

我有一个批处理脚本,用于从游戏服务器上的日志中提取聊天行。

  12/30/2015 12:42:03:#[PLAYERNAME]您好! 

然后处理日志的每一行以提取玩家名称,如下所示:

$ (!LINE!)do(setNAME = %% b
$ b

  for / ftokens = 2 delims = []如果玩家在他们的名字中没有[]括号字符,这个工作正常。  


p>

如果他们的名字如下所示:

  12/30/2015 12:42:03:#[<] [PLAYERNAME] [>]你好! 

或者任何类似的东西,我只提取小部分,而不是他们的完整名称[PLAYERNAME
$ b

我怎样才能让我的FOR命令帐户中包含玩家名字的任意数目的括号?



任何帮助将不胜感激。

解决方案

这个方法假设玩家的名字以 ](右方括号+空格)。

  @echo off 
setlocal EnableDelayedExpansion

for / Fdelims =%% a in(test.txt)do(
setLINE = %% a
echo LINE =!LINE!

电话:splitName

echo NAME =!NAME!

goto:EOF

:splitName
setNAME =%LINE:*#[=%
setNAME =%NAME:] =& rem%
exit / B

输出:

  LINE =12/30/2015 12:42:03:#[<] [PLAYERNAME] [>]你好
NAME = <] [PLAYERNAME] [> 中


I have a batch script I made to extract chat lines from a log on my gaming server.

The input file has text lines like this:

12/30/2015 12:42:03 : #[PLAYERNAME] Hello!

I then process each line of the log to extract the player name as follows:

for /f "tokens=2 delims=[]" %%d in ("!LINE!") do (set "NAME=%%d")

This works fine IF the player has no [] bracket characters in their name.

If their name looks like the following:

12/30/2015 12:42:03 : #[<][PLAYERNAME][>] Hello!

Or anything similar, then I only extract small parts and not their complete name of <][PLAYERNAME][>.

How could I make my FOR command account for any number of brackets that the player's name might have?

Any help would be appreciated.

解决方案

This method assume that the player name ends in "] " (right-square-braquet + space). Of course, if the player name may have these characters, the method will fail.

@echo off
setlocal EnableDelayedExpansion

for /F "delims=" %%a in (test.txt) do (
   set "LINE=%%a"
   echo LINE="!LINE!"

   call :splitName

   echo NAME="!NAME!"
)
goto :EOF

:splitName
set "NAME=%LINE:*#[=%"
set "NAME=%NAME:] =" & rem "%"
exit /B

Output:

LINE="12/30/2015 12:42:03 : #[<][PLAYERNAME][>] Hello"
NAME="<][PLAYERNAME][>"

这篇关于如何使用FOR批处理命令查找两个特定符号之间的所有文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆