批处理脚本以提取两个给定单词之间的行 [英] Batch Script to extract lines Between two given words
问题描述
我需要从文件中提取两个给定单词之间的文本.
I need to extract texts between two given words from a file.
some lines
<name>text1</name>
some lines
some lines
<name>text2</name>
some lines
<name>text3</name>
some more lines
-
我需要提取每个名称标签之间出现的所有文本
I need to extract all the occurrences of texts that occur between each of the name tags
<name> extract this text here </name>
- text1
- text2
- text3
谢谢.
推荐答案
以下脚本提取作为命令行参数提供的文件的所需标签之间的文本:
The following script extracts the text in between the desired tags of the file(s) provided as command line argument(s):
@echo off setlocal EnableExtensions DisableDelayedExpansion rem // Resolve command line arguments: for %%F in (%*) do ( rem // Read a single line of text following certain criteria: for /F "delims=" %%L in (' findstr /R "^[^<>]*<name>[^<>][^<>]*</name>[^<>]*$" "%%~F" ') do ( set "LINE=%%L" rem /* Extract the desired string portion; rem the preceding `_` is inserted for the first token rem never to appear empty to the `for /F` loop: */ setlocal EnableDelayedExpansion for /F "tokens=3 delims=<>" %%K in ("_!LINE!") do ( endlocal rem // Return found string portion: echo(%%K ) ) ) endlocal exit /B
这仅在只有一个标签
<name>
,然后是一些文本本身不包含<
和>
,然后是一个标签</name>
的情况下才有效;该字符串必须在一行上,并且某些文本本身不能包含<
和>
.This works only if there is exactly one tag
<name>
, followed by some text not containing<
and>
on its own, followed by exactly one tag</name>
; this string must be on a single line and may be preceded or followed by some texts not containing<
and>
on their own.这篇关于批处理脚本以提取两个给定单词之间的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!