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

查看:756
本文介绍了使用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天全站免登陆