批量选择文本中的字符串

时间:2021-12-18 02:07:30

I will give an example of what i need to do

我将举例说明我需要做些什么

Let's say i have this text file which contains a string

假设我有这个包含字符串的文本文件

some text some text some text
some text some text some text
*****************************
Your info is  :  random_value
*****************************
some text some text some text
some text some text some text

I want a batch script to search through text and find the random_value and then return it into a variable.

我想要一个批处理脚本来搜索文本并找到random_value然后将其返回到变量中。

Thanks!

1 个解决方案

#1


2  

You will need a pattern to match the line, and also a delimitter to split the matched string to get the desired value (If the string have spaces), you can do it with a FOR /F Loop:

您将需要一个匹配该行的模式,还需要一个分隔符来分割匹配的字符串以获得所需的值(如果该字符串有空格),您可以使用FOR / F循环:

@Echo OFF

Set "File=.\File.txt"
Set "Match=Your info is"
Set "Tokens=2"
Set "Delimitter=:"

For /F "Tokens=%Tokens% Delims=%Delimitter%" %%# in (
    'Type "%File%"^|FIND /I "%Match%"'
) Do (
    Set "Value=%%#"
)

Echo %VALUE%

Pause&Exit

PS: Tested with your example.

PS:用你的例子测试过。

#1


2  

You will need a pattern to match the line, and also a delimitter to split the matched string to get the desired value (If the string have spaces), you can do it with a FOR /F Loop:

您将需要一个匹配该行的模式,还需要一个分隔符来分割匹配的字符串以获得所需的值(如果该字符串有空格),您可以使用FOR / F循环:

@Echo OFF

Set "File=.\File.txt"
Set "Match=Your info is"
Set "Tokens=2"
Set "Delimitter=:"

For /F "Tokens=%Tokens% Delims=%Delimitter%" %%# in (
    'Type "%File%"^|FIND /I "%Match%"'
) Do (
    Set "Value=%%#"
)

Echo %VALUE%

Pause&Exit

PS: Tested with your example.

PS:用你的例子测试过。