If using Gnu-awk gawk is not an option you'll need a combination of the already mentioned for /f
and findstr
which has limited RegEx capabilities.
for /f will ignore leading delimiters and treat consecutive delimiters as only one. To parse your sample line you need the delimiters ()
and space
. The required numbers are tokens 2
and 4
, tokens 1
and 3
are acceptable variable names. A RegEx for the line could be "CURRENTHEIGHT([0-9][0-9]*).*TEMPERATURE([0-9][0-9]*)"
I DID say linmited ;-). The first used for variable %%A
corresponds to the first token; %%D
to the fourth one. To put all this together:
:: ExtractHeightTemp.cmd ::::::::::::::::::::::::::::::::::::::::::::
@echo off
Set File=C:\Test\test.txt
for /f "tokens=1-4 delims=() " %%A in (
'findstr /i "CURRENTHEIGHT([0-9][0-9]*).*TEMPERATURE([0-9][0-9]*)" "%File%"'
) do Set _%%A=%%B&Set _%%C=%%D
Set _
Pause
The batch produces this output here:
> ExtractHeightTemp.cmd
_CURRENTHEIGHT=9182
_TEMPERATURE=1234
1
Please note that https://superuser.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?.
– DavidPostill – 2016-10-19T21:21:59.083Hints: findstr - Search for strings in files. for /f - Loop command against the results of another command.
– DavidPostill – 2016-10-19T21:22:45.993In Windows, you use awk exactly as in other OS. However, awk is not installed by default; you need to find and download a Windows package of posix utilities. – ddbug – 2016-10-19T22:11:34.243