简体   繁体   English

Windows批处理文件,在标记之间找到一个字符串

[英]Windows batch-file, find a string between tag

i need to search within a file the first occurrence of a string. 我需要在文件中搜索字符串的首次出现。

I have this php file: 我有这个php文件:

<?php

#---------------------------------------------------------------------------------------
#
#
#  Gpl V2 and further license updates applies to this code
#
#  2015/05/21
#
#-------------------------------------------------------------------------------

error_reporting(0);

$version = '1.0 Alpha';

//some code
echo "\r\n===== ".$argv[0]." ".$version.": Select a channel =====: ";

I need to find this value 1.0 Alpha . 我需要找到此值1.0 Alpha。 In a bat file i have write this code. 在bat文件中,我已编写此代码。

for /f "tokens=*" %%x in ('findstr "$version" %~1') do (
set str=%%x
)

But he found all second string: echo "\\r\\n===== ".$argv[0]." ".$version.": Select a channel =====: "; 但是他发现了所有第二个字符串:echo“ \\ r \\ n =====”。$ argv [0]。“”。$ version。“:选择一个频道=====:”;

Solutions? 解决方案?

One last thing, the script could change from $version = '1.0 Alpha'; 最后一件事,脚本可以从$version = '1.0 Alpha';更改$version = '1.0 Alpha'; to $version = "1.0 Alpha"; $version = "1.0 Alpha";

The quotes may change. 报价可能会改变。

You need to exit the loop after finding the first occurrence of the string. 找到字符串的第一个匹配项后,您需要退出循环。 Try this: 尝试这个:

for /f "tokens=*" %%x in ('findstr "$version" %~1') do (
   set str=%%x
   goto :EndFor
)
:EndFor

set str=%str:"='% 
for /f "tokens=2 delims='" %%x in ('echo(%str%') do set str=%%x
echo(str=%str%
pause

This can be done in a single step, letting the FOR /F loop parse out the actual version string. 这可以一步完成,让FOR / F循环解析出实际的版本字符串。

The odd syntax specifies ' and " as token delimiters, and looks for the 2nd token. 奇怪的语法指定'"为标记分隔符,并寻找第二个符号。

@echo off
setlocal
for /f tokens^=2^ delims^='^" %%V in ('findstr $version "%~1"') do (
  set "version=%%V
  goto :break
)
:break
echo version=%version%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM