For example I have this HTTP Response:
{
"location": {
"lat": 51.0,
"lng": -0.1
},
"accuracy": 1200.4
}
How do I get only the values 51.0 and -0.1? I tried doing it with this:
for /F "tokens=3 delims= " %%a in (foo1.tmp) do (
echo %%a >> url.tmp
)
set /p lat=<url.tmp
more +1 <url.tmp >new.tmp
for /F "tokens=* delims= " %%a in (new.tmp) do (
set var2=%%a
)
I was able to get the first value which was 51.0 but for the second value which as var2, I am getting the 1200.4 value which is supposed to be -0.1. Any help here?
To better explain what my one liner from the comment does:
findstr "lat lng" <foo1.tmp
filters lines containing either lat or lng using default regular expression mode, sample output:
"lat": 51.0,
"lng": -0.1
Instead of using a temporary file, you can directly use curl/wget
To process the output the for /f parses the line with the delimiters :, (colon,comma,space).
Leading delims are ignored, adjacent ones are counted as only one, so
%%A contains "lat" / "lng"
%%B contains 51.0 / "-0.1"
To strip the double quotes from %%A the for variable modifier ~ is used.
In summary the (batch) line:
for /f "tokens=1,2 delims=:, " %%A in ('findstr "lat lng" ^<foo1.tmp') do set "%%~A=%%B"
sets the variables
> set l
lat=51.0
lng=-0.1
Related
I'm trying to recuperate a particular data from a file (we will call it MyFile.dart) using a batch file.
To be more precise I'm trying to recuperate the 2.4 from this line :
static var version = 2.4;
First, I use a for loop to iterate through each line of my file :
FOR /F "tokens=*" %%i IN (MyFile.dart)
Then I want to check if the line contains a particulare string (here "var version")
set str = %%i
if not %str:"var version"=% == %str% DO
I got this from this topic but here I get the error :
=str is unexpected
Since the check doesn't work, I comment it and I try my next for loop on each line of MyFile.dart (if the check worked it would have been only on the line containing "var version") :
set str = %%i
FOR /F "tokens=2 delims==" %%a IN (%str%) DO (
#echo %%a
)
Here I'm supposed to split the line using "=" as a separator and display the second element of the split array, but I get nothing printed in the console, and when I comment #echo off, I see that %str% is null. I tried using directly %%i but I also get an error.
So I hardcoded the line I'm interested in the loop :
set str = %%i
FOR /F "tokens=2 delims==" %%a IN ("static var version = 2.4;") DO (
#echo %%a
)
And got the expected result of 2.4; in the console (but obviously it's not how I want to get it).
So to summarize :
First problem : the "if not" to check if the line contains a particular substring doesn't work.
Second problem : I can't pass the variable from the first loop (a line of the file) to the second loop to then parse it.
Here is my whole code :
FOR /F "tokens=*" %%i IN (MyFile.dart) DO (
set str = %%i
if not %str:"var version"=% == %str% DO (
FOR /F "tokens=2 delims==" %%a IN (%str%) DO (
#echo %%a
)
)
)
NB : If you have a totally different algorithm to get to the same result I will take it !
set str = %%i
This sets the variable "strSpace" to the value "Space(the value of %%i)"
Use the syntax
set "str=%%i"
Including the quotes. Use set "var1=value" for setting STRING values - this avoids problems caused by trailing spaces. Quotes are not needed for setting arithmetic values (set /a`)
if not %str:"var version"=% == %str% DO (
The correct syntax is
if not "%str:var version=%" == "%str%" (
or, better
if "%str:var version=%" neq "%str%" (
The comparison is literal - both sides of the comparison operator must be quoted since the value may contain separators like spaces.
The correct syntax for string substitution is %varname:string=substitutestring%
Why set str again?
To parse a string using = as a delimiter, use
FOR /F "tokens=2 delims==" %%a IN ("string") DO (
Note however that %str% will be the value str had at the time the outer loop (%%i) was encountered. For an explanation of how this works, see Stephan's DELAYEDEXPANSION link
You should consider using
FOR /F "tokens=2 delims==" %%a IN ("%%i") DO (
This worked for me - I changed the name of the file
#ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=*" %%i IN (q71930885.txt) DO (
set "str=%%i"
if "!str:var version=!" neq "!str!" (
FOR /F "tokens=2 delims==" %%a IN ("%%i") DO ( #echo %%a )
)
)
GOTO :EOF
Response:
Space2.4;
I have 10000 tiny tmp/ txt files that look like these:
1.tmp: {"a": "you","data": "1","data2": "2"} <linefeed> {"b":"bo"}
2.tmp: {"a": "you2","data": "1","data2": "2"} <linefeed> {"b":"bo2"}
3.tmp: {"a": "you3","data": "1","data2": "2"} <linefeed> {"b":"bo3"}
How can I read each of them and convert these to another format, one file that has 1 row per file:
{ "a": "you", "b": "bo" },
{ "a": "you2", "b": "bo2" },
{ "a": "you3", "b": "bo3" },
The tricky part might be that each .tmp file has a linefeed?
My code starts
for /L %%i in (0,1,10000) do (call parsesomehow %%i.tmp )
As I understood there were only 2 lines in every tmp file.
You have to read every tmp file. In every cycle you should parse 1-st string. Then - 2-nd. This parce cycle must be the stored procedure
Next step is print parsed values to output file.
#echo off
for %%i in (*.tmp) do call :parse %%i
goto :EOF
:parse
for /f "delims=," %%a in (%1) do set "A=%%a" &goto NXT
:NXT
for /f "delims={" %%a in ('more +1 %1') do set "B=%%a"
echo %A%, %B%>>output.txt
I have a file links.txt:
1 a b c
21 b d
14 c j k l
5 d r e
test.bat 1:
#echo off
for /f "tokens=*" %%c in ('findstr /b "%*" links.txt') do (
echo %%c
)
I passed "1" as parameter to test.bat and expected output is "1 a b c". But it is displaying all the rows which is beginning with "1".
Your command is "find all of the lines in the file that /b (begin) 1 (passed in as a parameter).
You need
for /f "tokens=*" %%c in ('findstr /b /c:"%1 " links.txt') do (
as the syntax of findstr requires /c:"find this string" /c:"or this string" to find an exact string containing spaces - "find this string" will find find or this or string
You could process your input-parameter string like:
SET "params= %*"
SET "params=%params: = " /c:"% ""
SET "params=%params:~2%"
findstr /b %params% ....
OR perhaps use
findstr /b /g:"afilename" ...
where afilename contains your required strings one to a line with the spaces appended as required. Note that many editors will drop trailing spaces by default.
For the example case in the comment...
#echo off
setlocal
SET "params= %*"
SET "params=%params: = " /c:"% ""
SET "params=%params:~2%"
echo the params string is %params%
for /f "tokens=*" %%c in ('findstr /b /L %params% links.txt') do (
echo %%c
)
params is assigned the value of the command-tail. Each space is then replaced by " /c:" and " appended to the end. Then only those characters after the second are used.
I need help to make a batch code (if it's possible) to get substring from filename.
My filename can be like (filename lenght is changing):
7_D_D1_012345678-2015-07-07.pdf
8_A_087654321-2015-07-07.pdf
10_D_D1_011122558-2015-07-07.pdf
100_C_CCC1_C2_C3_C4_055555555-2015-07-07.pdf
file number - from left to first _
id1 - from 1 to n string with _ separator; for example C_C1_C2_C3_C4
id2 - always 9 digits; for example 011122558
date - for example 2015-07-07
extension .jpg
How to loop substring (file number, id1, d2, date) for all filenames in folder and put it to my code
convert - "file number" -annotate "id1" -annotate2 "id2" -annotate "date"
for example:
convert - "01" -annotate "C_C1" -annotate2 "012345678" -annotate "2015-07-07"
Thanks for help.
pure batch. Simple string manipulation mixed with tokenization. No need for additional utilities.
(g.txt holds your example file names; could be replaced by 'dir /b /a-d')
#echo off
for /f %%i in (g.txt) do call :process %%i
goto :eof
:process
set x=%1
set ext=%x:*.=%
for /f "delims=_" %%i in ("%x%") do set fileno=%%i
for /f "tokens=1,*delims=-" %%i in ("%x%") do (
set x1=%%i
set x2=%%j
)
for /f "tokens=1,* delims=." %%i in ("%x2%") do (
set dat=%%i
set ext=%%j
)
set id2=%x1:~-9%
for /f "tokens=1,* delims=_" %%i in ("%x1:~0,-10%") do set id1=%%j
echo filename %x%
echo ------------------------
echo Nr. %fileno%
echo ID1 %id1%
echo ID2 %id2%
echo Date %dat%
echo Ext. %ext%
echo ------------------------
echo convert - "%fileno%" -annotate "%id1%" -annotate2 "%id2% -annotate "%dat%"
echo(
echo(
goto :eof
Since you said Windows 7, I know you have Powershell available. Here is a Powershell script:
$re = '^(\d+)_((?:(?:[a-zA-Z0-9]+)_?)+)_(\d{9})-(\d{4}-\d\d-\d\d)\.(\w+)$'
dir | ForEach-Object {$_ -replace $re, 'convert "$1" -annotate "$2" -annotate2 "$3" -annotate3 "-$4"'}
Given the filenames you gave in your question
7_D_D1_012345678-2015-07-07.pdf
8_A_087654321-2015-07-07.pdf
10_D_D1_011122558-2015-07-07.pdf
100_C_CCC1_C2_C3_C4_055555555-2015-07-07.pdf
It will produce this text output:
convert "100" -annotate "C_CCC1_C2_C3_C4" -annotate2 "055555555" -annotate4 "2015-07-07"
convert "10" -annotate "D_D1" -annotate2 "011122558" -annotate4 "2015-07-07"
convert "7" -annotate "D_D1" -annotate2 "012345678" -annotate4 "2015-07-07"
convert "8" -annotate "A" -annotate2 "087654321" -annotate4 "2015-07-07"
(The filenames were sorted first, so the one starting with 100 comes first and the one starting with 8 comes last).
By redirecting this text output into a .cmd file, you can execute the convert commands as desired.
Here is the breakdown of that regular expression:
Beginning of line or string
[1]: A numbered capture group. [\d+]
Any digit, one or more repetitions
_
[2]: A numbered capture group. [(?:(?:[a-zA-Z0-9]+)_?)+]
Match expression but don't capture it. [(?:[a-zA-Z0-9]+)_?], one or more repetitions
(?:[a-zA-Z0-9]+)_?
Match expression but don't capture it. [[a-zA-Z0-9]+]
Any character in this class: [a-zA-Z0-9], one or more repetitions
_, zero or one repetitions
_
[3]: A numbered capture group. [\d{9}]
Any digit, exactly 9 repetitions
-
[4]: A numbered capture group. [\d{4}-\d\d-\d\d]
\d{4}-\d\d-\d\d
Any digit, exactly 4 repetitions
#echo off
setlocal enableextensions disabledelayedexpansion
rem For each file
for /r "x:\starting\folder" %%z in (*.pdf) do (
rem Separate number part
for /f "tokens=1,* delims=_" %%a in ("%%~nz") do (
set "_number=%%~a"
set "_file=%%~fz"
rem Separate date and ids
for /f "tokens=1,* delims=-" %%c in ("%%~b") do (
set "_date=%%~d"
set "_ids=%%~c\."
)
)
rem Separate id1 from id2 handling the string as a path
rem This way id2 is the last element and the path to it
rem is id1
setlocal enabledelayedexpansion
for /f "delims=" %%e in ("::!_ids:_=\!") do (
endlocal
set "_id2=%%~nxe"
set "_id1=%%~pe"
)
rem Correct id1 contents (it is a path) changing backslashes
rem to underscores. As there are initial and ending backslashes,
rem later we will remove the initial and ending underscores
setlocal enabledelayedexpansion
for /f "delims=" %%e in ("!_id1:\=_!") do (
endlocal
set "_id1=%%~e"
)
rem Execute final command
setlocal enabledelayedexpansion
echo(
echo file[!_file!]
echo convert - "!_number!" -annotate "!_id1:~1,-1!" -annotate2 "!_id2!" -annotate "!_date!"
endlocal
)
FOR /F "tokens=1-7 delims=," %%G IN (C:\Users\asian\Desktop\FOR_LOOP\1sv1.csV) do echo %%G,%%H,%%I >> 1.csv
output:
20030701,1001,456.000000
20030701,1002,459.000000
20030701,1003,450.000000
20030701,1004,456.000000
20030701,1005,459.000000
my Query is that i want to divide value for %%I variable(456.000000)
output should be like this:
20030701,1001,228.000000
20030701,1002,229.5.000000
20030701,1003,225.000000
20030701,1004,228.000000
20030701,1005,229.5.000000
setlocal enabledelayedexpansion
(FOR /F "tokens=1-7 delims=," %%G IN (C:\Users\asian\Desktop\FOR_LOOP\1sv1.csV) do (
set /a "i=%%I*5"
echo %%G,%%H,!i:~0,-1!.!i:~-1!00000
)) > 1.csv
endlocal
Batch arithmetics has no decimals. So, to handle it, the value is multiplied by 10 (to gain an aditional position) and divided by 2 (from OP). Then, the value is splitted as the last digit is the first decimal.