简体   繁体   中英

How to get a number from a text file after a specific string in one of the lines somewhere on the line?

I have a text file which contains the following two lines:

-host MYPC -param 3 -param2 4
-host MYPC -param3 2 -param4 5

I want to get the value corresponding to -param which is 3 ( -param 3 ) in a Windows batch file.

I tried that with the findstr command as shown below:

findstr /R "^-param$" > step1_a.txt my_file.txt

But it output this:

FINDSTR: /c ignored
FINDSTR: /A ignored
FINDSTR: /f ignored
FINDSTR: /f ignored
FINDSTR: /t ignored
FINDSTR: /y ignored
FINDSTR: /a ignored
FINDSTR: /k ignored

I think, this is caused by - in the string.

How to get the value 3 associated with -param in my text file?

You used anchors ^ and $ which mark begining of a line and end of a line (see findstr docs ).

Correct pattern would be: findstr /r /c:"-param [0-9]*"

I used additional flag /c , so you can use spaces inside the pattern and they will not be treated as "or" operator.

Pattern is simple: first match -param literally, then match zero or more digis with [0-9]*

Here is an example code on how to process not really fail safe, but hopefully good enough for your purpose, the lines read from a text file to get the next argument string after a well-known argument string like -param .

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "DataFile=%~dp0my_file.txt"

rem Does the input data file exist?
if exist "%DataFile%" goto ProcessData
rem Input data file not found in directory of the batch file.
echo ERROR: Could not find file: "%DataFile%"
goto :EOF

:ProcessData
set "ParamValue="
for /F usebackq^ delims^=^ eol^= %%I in ("%DataFile%") do for %%J in (%%I) do (
    if not defined ParamValue (
        if /I "%%~J" == "-param" set "ParamValue=1"
    ) else (set "ParamValue=%%~J" & goto HaveValue)
)
rem The parameter of interest was not found at all or there is no value.
echo ERROR: Could not find the parameter with name: "-param"
goto :EOF

:HaveValue
rem Output the parameter value as an example for further command lines.
set ParamValue

endlocal

The outer FOR loop reads non-empty lines one after the other from text file and assigns each line completely to the specified loop variable I .

The inner FOR loop processes the current line similar to how cmd.exe processes the argument strings passed to a batch file. All space/tab/comma/semicolon/equal sign/non-breaking space (in OEM encoding) delimited strings are ignored until a string is found which is case-insensitive equal the string -param . The next string in the current line is assigned to the environment variable ParamValue and the two loops are exited with the command GOTO to continue batch file processing on the line below the label :HaveValue where the environment variable ParamValue can be used for whatever purpose.

This extended version of above gets first the string after -param which is in the example 3 . Then the entire text file is searched again for an argument string starting with -param and the string appended which was read first from file which is in the example -param3 . If this string is found, the next string is assigned to environment variable ParaValue which is 2 in the example.

@echo off
set "DataFile=%~dp0my_file.txt"

rem Does the input data file exist?
if exist "%DataFile%" goto ProcessData
rem Input data file not found in directory of the batch file.
echo ERROR: Could not find file: "%DataFile%"
goto :EOF

:ProcessData
set "ParamName="
for /F usebackq^ delims^=^ eol^= %%I in ("%DataFile%") do for %%J in (%%I) do (
    if not defined ParamName (
        if /I "%%~J" == "-param" set "ParamName=1"
    ) else (set "ParamName=-param%%~J" & goto HaveName)
)
rem The parameter of interest was not found at all or there is no value.
echo ERROR: Could not find the parameter with name: "-param"
goto :EOF

:HaveName
set "ParamValue="
for /F usebackq^ delims^=^ eol^= %%I in ("%DataFile%") do for %%J in (%%I) do (
    if not defined ParamValue (
        if /I "%%~J" == "%ParamName%" set "ParamValue=1"
    ) else (set "ParamValue=%%~J" & goto HaveValue)
)
rem The parameter of interest was not found at all or there is no value.
echo ERROR: Could not find the parameter with name: "%ParamName%"
goto :EOF

:HaveValue
rem Output the parameter value as an example for further command lines.
set ParamValue

endlocal

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /? ... explains %~dp0 ... batch file path ending always with a backslash.
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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