使用 Windows 批处理提取文本文件的一部分 [英] Extract part of a text file using Windows batch

查看:30
本文介绍了使用 Windows 批处理提取文本文件的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,我有兴趣只拥有其中的一部分,包含在 wordA 和 wordB 之间的所有内容

I have a text file and I'm interested to have only a part of it, all that is included between wordA and wordB

可以使用 cmd 批处理文件吗?

Is that possible using a cmd batch file?

推荐答案

这是一种方法.假设 wordA 和 wordB 在同一行(两者都必须存在).子程序 :FindString 删除不需要的前面的文本,然后用我们不希望在文本中用作分隔符的字符替换wordB"(在本例中为`).如有必要,请使用其他字符.

Here's one way to do it. Assuming that wordA and wordB are on the same line (both must be present). The subroutine :FindString removes undesired preceding text and then replaces "wordB" with a character that we don't expect in the text that we will then use as a delimiter (` in this case). use another character if necessary.

@ECHO OFF
SET InFile=Test.txt

FOR /F "tokens=*" %%A IN ('FINDSTR "wordA" "%InFile%" ^| FINDSTR "wordB"') DO CALL :FindString "%%A"
pause
GOTO :eof

:FindString
SET String=%~1
SET String=%String:*wordA =%
SET String=%String: wordB=`%
FOR /F "tokens=1 delims=`" %%A IN ('ECHO.%String%') DO ECHO.%%A]
GOTO :eof

这篇关于使用 Windows 批处理提取文本文件的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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