I have to parse the following
/*-secure- {"challenges":{"wl_antiXSRFRealm":{"WL-Instance-Id":"3mva6l4ph7816571thcnc391vj"}}}*/
and extract 3mva6l4ph7816571thcnc391vj
for /f usebackq^ tokens^=8^ delims^=^" %%a in ("response.txt") do echo %%a
Use the quote as delimiter and get the required field
#echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%i in ('find /i "child2" test.txt') do (
set a=%%i
set a=!a:*child2":"=!
set a=!a:"}}}*/=!
)
echo %a%
set a=!a:*child2":"=! removes all from beginning of the line including child2":"
set a=!a:"}}}*/=! removes the string "}}}*/ at the end
With a cleaned input like this:
{"challenges":{"wl_antiXSRFRealm":{"WL-Instance-Id":"3mva6l4ph7816571thcnc391vj"}}}
This should work:
jq -r .challenges.wl_antiXSRFRealm."WL-Instance-Id" response.txt
Output:
3mva6l4ph7816571thcnc391vj
Related
I have a CSV file with leading-and-trailing doublequotes per line I want to remove, and made a DOS batch to do it. The following works for an explicit path:
#echo off
setlocal enabledelayedexpansion
for /F "tokens=*" %%A in (C:\Folder\WrappedInQuotes.csv) do (
set line=%%A
echo !line:~1,-1! >> C:\Folder\UnWrapped.csv
)
Of course, if the path has spaces in it, the following will not work:
#echo off
setlocal enabledelayedexpansion
for /F "tokens=*" %%A in (C:\Folder\Sub Folder\WrappedInQuotes.csv) do (
set line=%%A
echo !line:~1,-1! >> C:\Folder\Sub Folder\UnWrapped.csv
)
(#echo on, the message is "...cannot find the file C:\Folder\Sub", of course)
As a next-step test, I simply wrapped the two explicit filespecs in doublequotes:
#echo off
setlocal enabledelayedexpansion
for /F "tokens=*" %%A in ("C:\Folder\Sub Folder\WrappedInQuotes.csv") do (
set line=%%A
echo !line:~1,-1! >> "C:\Folder\Sub Folder\UnWrapped.csv"
)
With #echo on, the For seems to be getting the correct filespec (original CSV), but now the destination CSV has
:\Folder\Sub Folder\EachLineWrappedInQuotes.cs
(the source CSV full filespec, with first and last characters removed), instead of the contents of the source CSV with first and last characters (the doublequote wrapping) removed.
Ultimately, I want to replace the explicit paths with a path variable like %~dp0, but haven't been able to get past the "next-step test".
(I have tried to solve this by studying the many answers already given, with no success, sorry!)
To get the content of the file and remove double quotes, without the need to set variables, set usebackq
method 1:
#echo off
for /F "usebackq tokens=*" %%A in ("C:\Folder\Sub Folder\WrappedInQuotes.csv") do (
echo %%~A >> "C:\Folder\Sub Folder\UnWrapped.csv"
)
method 2, if you still want to set the variable:
#echo off
setlocal enabledelayedexpansion
for /F "usebackq tokens=*" %%A in ("C:\Folder\Sub Folder\WrappedInQuotes.csv") do (
set line=%%~A
echo !line! >> "C:\Folder\Sub Folder\UnWrapped.csv"
)
Or by using type :
#echo off
for /F "tokens=*" %%A in ('type "C:\Folder\Sub Folder\WrappedInQuotes.csv"') do (
echo %%~A >> "C:\Folder\Sub Folder\UnWrapped.csv"
)
I am trying to inject values to a dynamic values I received from outside a batch script.
To be more clear here is an example:
I have a property file named prop.properties that contain 2 values:
prop.properties
path1=%value1%/...
path2=%value1%/...
in addition I have a batch myFile.bat file that read the property file.
myFile.bat
echo off
set value1=%1
For /F "tokens=1* delims==" %%A IN (prop.properties) DO (
IF "%%A"=="path1" set val1=%%B
IF "%%A"=="path2" set val2=%%B
)
echo %var1%
echo %var2%
so after running myFile.bat C:
I will get in the output:
C:/...
c:/...
or if I run:
myFile.bat D:
I will get:
D:/...
D:/...
If I understand what you're trying to do then use delayed variable expansion:
Change percent to exclamation point in prop.proerties
path1=!value1!/...
path2=!value1!/...
then:
#echo off
setlocal EnableDelayedExpansion
set value1=%1
For /F "tokens=1* delims==" %%A IN (prop.properties) DO (
IF "%%A"=="path1" set var1=%%B
IF "%%A"=="path2" set var2=%%B
)
echo %var1%
echo %var2%
Though there are so many other things/questions.
Here's one idea:
#echo off
setlocal EnableDelayedExpansion
set value1=%1
For /F "tokens=1* delims==" %%A IN (prop.properties) DO (
set var_%%A=%%B
)
echo %var_path1%
echo %var_path2%
set var_
for /f "tokens=2 delims==" %%a in ('set var_') do echo %%a
Though I don't know why you don't just:
prop.properties
path1=/...
path2=/...
#echo off
for /F "tokens=1* delims==" %%A IN (prop.properties) DO echo %value1%%%A
I want to read a text file and store each line in an array. When I used the code below, "echo %i%" is printing 0 every time and only array[0] value is getting assigned. But in "set n=%i%",n value is assigned as the last incremented I value.Also "#echo !array[%%i]!" is printing like !array[0]! instead of printing the value. Is there any syntax error in the code?
set /A i=0
for /F %%a in (C:\Users\Admin\Documents\url.txt) do (
set /A i+=1
echo %i%
set array[%i%]=%%a
)
set n=%i%
for /L %%i in (0,1,%n%) do #echo !array[%%i]!
Here's a method that is useful at times and very similar to your code:
#echo off
set "file=C:\Users\Admin\Documents\url.txt"
set /A i=0
for /F "usebackq delims=" %%a in ("%file%") do (
set /A i+=1
call echo %%i%%
call set array[%%i%%]=%%a
call set n=%%i%%
)
for /L %%i in (1,1,%n%) do call echo %%array[%%i]%%
#echo off &setlocal enabledelayedexpansion
for /F "delims=" %%a in (C:\Users\Admin\Documents\url.txt) do (
set /A count+=1
set "array[!count!]=%%a"
)
for /L %%i in (1,1,%count%) do echo !array[%%i]!
Inside a code block you need delayed expansion and !variables!.
Read set /? description about environment run-time linking. When you are using %i% inside for - it is pre-expanded before for execution. You need to use !i! instead.
#ECHO OFF
SETLOCAL
FOR /f "tokens=1*delims=:" %%i IN ('findstr /n /r "$" url.txt') DO SET max=%%i&SET array[%%i]=%%j
FOR /l %%i IN (1,1,%max%) DO CALL ECHO(%%array[%%i]%%
GOTO :EOF
provided no line begins ":"
So the problem is that I will get filename such as:
"a.b.c.d.e.f.g"
and I need to get the 2 last portions. In this case,
"f.g"
and I don't know how many dots the string will have.
Thx for your help!
#ECHO OFF
SETLOCAL
SET "fname=a.b.c.d.e.f.g"
FOR %%a IN (%fname%) DO FOR %%b IN (%%~na) DO ECHO %%~xb%%~xa
try this:
#echo off &SETLOCAL
SET "fname=a.b.c.d.e.f.g"
:loop
SET /a t+=1
SET "d="
FOR /f "tokens=%t% delims=." %%a IN ("%fname%") DO SET "d=%%a"
SET "f=%g%"&SET "g=%e%"&SET "e=%d%"
IF DEFINED d GOTO :loop
ECHO %f%.%g%
€: works for all delimiters, not only ..
You could use the parameter modifier x in %~xI` to access only the extension and then use it twice.
#echo off
set "fname=a.b.c.d.e.f.g"
for /F "delims=" %%A in ("%fname%") DO (
set "ext2=%%~xA"
for /F "delims=" %%A in ("%%~nA") DO set "ext1=%%~xA"
)
echo %ext1%%ext2%
this is my myfile.txt I want to add space in second column as see the sample
ARK,LAR SNE,QNE,898,ILO,SNE,SNE,LAR,LAR,545
AUS,MNY P08,TTL,7776,STO,STL,STL,MNY,MNY,567
BOS,MTZ TNK,SDK,444,PPO,TNK,TNK,MTZ,MTZ,456
this is the code I am using
for /f "tokens=* " %%i in (myfile.txt) do call :echo2 %%i %%J %%K %%L %%M %%N %%O %%P %%Q %%R %%S
goto :EOF
:echo2
echo insert ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10'); >>myfile1.txt
goto :EOF
its displaying results , where it should have taken space what I am missing any help is appreciated
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
(
FOR /f "delims=" %%i IN (myfile.txt) DO (
SET "dataline=%%i"
SET "outline="
CALL :adddata
)
)>myfile1.txt
GOTO :EOF
:adddata
FOR /f "tokens=1*delims=," %%p IN ("%dataline%"
) DO SET outline=%outline%'%%p',&SET "dataline=%%q"
IF DEFINED dataline GOTO adddata
ECHO insert (%outline:~0,-1%);
GOTO :eof
This should do the job with no practical limit on columns - provided of course that the comma is reliably an end-of-column delimiter.
For each line in the source file, assign the entire line to
dataline and clear outline
then take the first token, delimited by comma, from dataline, quote it,add a comma and append it to outline; then set dataline to the remainder of the line after the first comma.
repeat until there is nothing left in dataline
output the text insert ( + all but the last character of outline (which will be a comma) + );
If I understand you correctly, you want to preserve the spaces in the text between the 1st and 2nd comma, correct? Try this:
#echo off
for /f "tokens=1-10 delims=," %%a in (myfile.txt) do (
>>myfile1.txt echo.insert ('%%a','%%b','%%c','%%d','%%e','%%f','%%g','%%h','%%i','%%j'^);
)
Try this:
#echo off & setlocal
(for /f "delims=" %%i in (myfile.txt) do (
set "line='%%i'"
setlocal enabledelayedexpansion
set "line=!line:,=','!"
set "line=!line: = ','!"
echo(insert (!line!^);
endlocal
))>myfile1.txt
You can't exceed 9 variables so your script won't work after the 9th. You can use for /f to copy each line exactly as the original file like so:
for /f "tokens=* " %%i in (myfile.txt) do echo %%i >>myfile1.txt
goto :EOF