Replace string % with %% using batch file - windows

I have a text like below in input.txt file.
sdf%5Ddfssdsd%2Ddfdf
I would like to replace "%" with "%%" in the output file. so the text should look like
sdf%%5Ddfssdsd%%2Ddfdf
Appreciate your help!!

#echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=1* delims=" %%i in (input.txt) do (
set _line=%%i
set _line=!_line:%%=%%%%!
echo !_line! >> output.txt
)
endlocal

Related

Windows Batch scripting(splitting of strings using multiple delimiters)

I have a property file (test.properties) which has a variable which holds multiple strings.
Ex: var=str1;str2;str3;.....
I need to use the above properties file in my batch file (abc.bat), so that i could print the values line by line. Output of the batch file should look like this...
str1
str2
str3
...
...
(and so on)
Any help could be appreciated..Thanx:)
Ive tried to use "for loop" to seperate the values from first delimiter(=) in this way...
IF EXIST "test.properties"
(
ECHO test.properties file found
for /F "tokens=1,2 delims==" %%A IN (test.properties) DO
(
set value="%%B"
ECHO !value!
)
)
Output=str1;str2;str3;....
Now if i want to parse the strings in "!value!" line by line i use ...
for /F "tokens=* delims=;" %%x IN ("!value!") DO
(
ECHO %%x
)
I am facing error.....Any help?
just use a plain for to get elements of a list (; is a standard delimiter)
#echo off
setlocal enabledelayedexpansion
>test.properties echo var=str1;str2;str3;str4;str5;str6
IF EXIST "test.properties" (
ECHO test.properties file found
for /F "tokens=1,2 delims==" %%A IN (test.properties) DO (
set "value=%%B"
ECHO !value!
)
for %%x IN (!value!) DO echo %%x
)

Windows - findstr in for loop (file content)

I have a text file which containts stuff like
M test123
S test
M abc
and so on...
I'm trying to write a batch script that will do the following:
Read this text file, search every line for "M " (with spaces!) and then save the found line in a variable, delete the "M " and store the output in a seperate output.txt
So the output.txt should containt then following:
test123
S test
abc
Here's what I have so far:
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN (output_whole_check.txt) DO (
SET var!count!=%%F
findstr /lic:"M " > nul && (set var!count!=var!count!:~8%) || (echo not found)
SET /a count=!count!+1
)
ENDLOCAL
Or is there some easier way to solve that without any additional stuff installed on windows?
Try this one. It echoes all lines to output.txt with "M " replaced with nothing.
#echo off & setlocal
>output.txt (
FOR /F "usebackq delims=" %%I IN ("output_whole_check.txt") DO (
set "line=%%I"
setlocal enabledelayedexpansion
echo(!line:M =!
endlocal
)
)
Result:
test123
S test
abc
Or if your output_whole_check.txt is very large, it might be faster to loop over the lines using a for /L loop. for /L is more efficient than for /F. You just have to get a count of the lines to determine how many iterations to loop.
#echo off & setlocal
rem // get number of lines in the text file
for /f "tokens=2 delims=:" %%I in ('find /v /c "" "output_whole_check.txt"') do set /a "count=%%I"
<"output_whole_check.txt" >"output.txt" (
for /L %%I in (1,1,%count%) do (
set /P "line="
setlocal enabledelayedexpansion
echo(!line:M =!
endlocal
)
)
The result is the same output.

Parsing in batch script

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

batch script to parse txt file with delimiter as semicolon

my text file is as shown below:
H;SEQUENCENUMMER;TIMESTAMP;VERSION;VALIDITY_FLAG;SID
D;1077;1383519656;"20.0.8-2";1;;
D;1079;1383519657;"20.0.8-2";2;;
i want to parse this using windows batch program and output file will look like as beloW:
H SEQUENCENUMMER TIMESTAMP VERSION VALIDITY_FLAG SID
D 1077 1383519656 "20.0.8-2" 1
D 1078 1383519657 "20.0.8-3" 2
something like a tab delimited
pls suggest
#echo off
setlocal enableextensions enabledelayedexpansion
rem Get a tab character
for /f tokens^=^*^ delims^= %%t in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0x09"') do set "tab=%%t"
rem For each line in text file, replace ; with a tab
(for /f "tokens=*" %%l in (plaintext.txt) do (
set "line=%%l"
echo !line:;=%tab%!
)) > output.txt
endlocal
#echo off
setlocal enabledelayedexpansion
if exist output.txt del output.txt
for /f "delims=" %%a in ('type "Your Text File.txt"') do (set $line=%%a
echo !$line:;= !>>output.txt)
You can use nested for-loops as follows:
#echo off
echo.|set a=>outfile.txt
for /f %%A in (infile.txt) do (
for %%B in (%%A) do (
echo.|set /P ="%%B ">>outfile.txt
)
echo.>>outfile.txt
)
You don't really seem to be parsing anything, you are just replacing semicolons with tabs. You can use VBScript to do this. Save the following as "process.vbs"
Option Explicit
Dim Line,Regex
Set RegEx = new RegExp
Regex.Pattern = ";"
Regex.Global=True
Do While Not WScript.StdIn.AtEndOfStream
Line = WScript.StdIn.ReadLine()
Line = Regex.Replace(Line,VBTab)
WScript.Echo Line
Loop
Then you can run it like this:
vbscript /nologo process.vbs < yourfile > newfile

Exchange parts of strings in a text file

is there a way to transfer characters in the a plain .txt file.
I have a lot of entries in the .txt file and they are all in this format:
1 = Example,
2 = Example2,
3 = Example3...
what I need is a batch file to transfer "# =" to the right side of the string. So it looks like this:
Example = 1,
Example2 = 2...
Is it possible to that with a .bat file?
This will handle more than one word where you have Example...
#echo off
for /f "tokens=1,* delims== " %%a in (input.txt) do >>output.txt echo %%b = %%a
If you can use GNU sed:
sed "s/\(\S\+\)\s=\s\(\S\+\)/\2 = \1/" file
Something like this should work:
#echo off
pushd "C:\some\where"
for /f "tokens=1,2 delims==" %%a in (input.txt) do echo %%b = %%a
popd
Or use this to get rid of whitespace:
#echo off
setlocal EnableDelayedExpansion
pushd "C:\some\where"
for /f "tokens=1,2 delims==" %%a in (input.txt) do (
set "key=%%a"
set "val=%%b"
echo !val: =! = !key: =!
)
popd
To save the output to a file, call either script like this:
switch.cmd >output.txt

Resources