简体   繁体   中英

How get specific word from the file using batch scripting?

I have a file of which the contents are as follows which are in consecutive lines,

VERSION=7.0.2
BUILD=03bbabbd5c0f
PRODUCT=splunk
PLATFORM=Windows-AMD64

From this I only want the VERSION. I tried using the following command:

FOR /F "usebackq tokens=1 eol=P" %G IN ("C:\ProgramFiles\SplunkUniversalForwarder\etc\splunk.version") DO echo %G

Involved eol=P because it doesn't bring out the last two lines, but I don't want the second line too. Can anyone help? Actually the main goal is to get only the version number not even the "VERSION=".

Along the lines of my comment.

At the Command Prompt:

For /F "UseBackQ EOL=P Tokens=1* Delims==" %A In ("%ProgramFiles%\SplunkUniversalForwarder\etc\splunk.version") Do @Echo %B

As a batch file:

For /F "UseBackQ EOL=P Tokens=1* Delims==" %%A In ("%ProgramFiles%\SplunkUniversalForwarder\etc\splunk.version") Do @Echo %%B

In a batch file use this code:

@echo off
for /F "usebackq tokens=1* delims==" %%A in ("C:\Program Files\SplunkUniversalForwarder\etc\splunk.version") do if /I "%%~A" == "VERSION" if not "%%~B" == "" set "Version=%%~B" & goto HaveVersion
echo Error: Could not find VERSION= with a version string in file:
echo        C:\Program Files\SplunkUniversalForwarder\etc\splunk.version
pause
goto :EOF

:HaveVersion
echo Version is: %Version%
pause

Please note the space in Program Files .

The command FOR with the used option /F reads the specified file line by line with skipping empty lines and lines starting with ; which is the default for eol (end of line) option.

The option usebackq is required to get the full qualified file name enclosed in double quotes interpreted as file name and not as string to process. The double quotes " around full qualified file name are required because of space character.

delims== redefines the delimiters for splitting the lines into substrings (tokens) from default space and horizontal tab to equal sign. So FOR splits now the lines using only = as delimiter character for the strings.

tokens=1* means that the first equal sign delimited substring should be assigned to loop variable A . And the rest of the line after the first equal sign(s) should be assigned without any further splitting to next loop variable according to ASCII table which is in this case the loop variable B . Now it should be also clear why loop variables are case-sensitive while environment variables are not case-sensitive. It makes a difference on what is the next loop variable if the specified loop variable is A or a .

On each loop run first a case-insensitive string comparison is made to check if the version string is at beginning of current line. If this first condition is true a second IF condition is used to verify that there is really a version string right to the equal sign in the file. If this second condition is also true the version string is assigned to environment variable Version and the loop is exited by continuing the batch file processing with a jump to the line below label HaveVersion . So the other lines in file are not further processed by FOR .

In general it is better to reference the environment variable ProgramFiles with %ProgramFiles% instead of using C:\\Program Files as the standard program files directory for 64-bit applications on Windows x64 respectively for 32-bit applications on Windows x86 can be on any drive with any folder name. But the Windows WOW64 Implementation Details must be taken into account if the batch file should work on 32-bit Windows, on 64-bit Windows in 64-bit environment and on 64-bit Windows in 32-bit environment. See also Wikipedia article about Windows Environment Variables .

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.

  • echo /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • set /?

See also Single line with multiple commands using Windows batch file for an explanation of operator & .

Simpler:

FOR /F "tokens=2 delims==" %G IN ('findstr "VERSION" "C:\ProgramFiles\SplunkUniversalForwarder\etc\splunk.version"') DO echo %G

Use findstr command to select the desired line, that may be at any position in the file...

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