Page 1 of 1
Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
Posted: 05 Apr 2019 06:21
by stondesign
Hi how can I convert a text.txt file containing the strings
1111 2222 3333
to
\x11\x11\x22\x22\x33\x33
Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
Posted: 05 Apr 2019 06:37
by aGerman
Code: Select all
@echo off &setlocal
set "in_file=test.txt"
set "out_file=test2.txt"
setlocal EnableDelayedExpansion
<"!in_file!" >"!out_file!" (
for /f %%i in ('type "!in_file!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (
set "line=" &set /p "line="
if defined line (
set "line=!line: =!"
echo \x!line:~0,2!\x!line:~2,2!\x!line:~4,2!\x!line:~6,2!\x!line:~8,2!\x!line:~10,2!
)
)
)
If that doesn't work as expected then better provide real data.
Steffen
Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
Posted: 05 Apr 2019 06:44
by stondesign
0000 d601 ac03 7805 0cfe 0cfe 0cfe 0cfe 9808 9808 9808 280a 280a 280a 280a 1c0c 1c0c 1c0c 10c0 ac0d ac0d ac0d ac0d 100e 100e 100e 100e 100e 100e 100e 100e 100e 100e 100e 100e de0d de0d de0d de0d 0cfe 0cfe 0cfe 0cfe 0cfe 0cfe 0cfe 0cfe 0cfe 0cfe 0cfe 0cfe e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 0a00
Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
Posted: 05 Apr 2019 06:46
by stondesign
I would need a generic script to transform even long strings
Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
Posted: 05 Apr 2019 06:50
by aGerman
Does the file consist of only one line? Have lines always the same length? What is the maximum length of a line?
Batch has so many limitations. The code we are able to write is only as good as the information you give.
Steffen
Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
Posted: 05 Apr 2019 06:58
by stondesign
The string may vary in length but the shape is 4 letters and numbers attached and then space and 4 more letters and numbers Now to overcome this problem I downloaded a tasker that stores the steps on the PC screen and repeats them over and over again
Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
Posted: 05 Apr 2019 07:02
by aGerman
aGerman wrote: ↑05 Apr 2019 06:50
Does the file consist of only one line? [...] What is the maximum length of a line?
Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
Posted: 05 Apr 2019 07:51
by Squashman
On StackOverFlow, when we see a broad question with limited information, we will direct the user to read the following two links:
How to ask a good question?
How to create a Minimal, Complete, and Verifiable example.
This seems like a good time to link to this information.
Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
Posted: 05 Apr 2019 09:42
by dbenham
Due to the many limitations and idiosyncrasies of batch, I rarely use pure batch to manipulate text files anymore.
I find it much easier to use
JREPL.BAT. Once you learn regular expressions and study the JREPL options, it is usually fairly easy to get the result you need, and it is significantly faster than pure batch.
For your little problem, I would use:
Code: Select all
call jrepl "\S\S|\s+" "\$&|" /t "|" /f "text.txt" /o -
This prepends \ before each pair of non-whitespace characters, and removes all whitespace from each line.
The above has a limitation that it will corrupt the result if you use it two or more times on the same file.
Below is a variation that does the conversion only when needed, and leaves already transformed files alone.
Code: Select all
call jrepl "([0-9a-f]{2})([0-9a-f]{2})|\s+" "\$2\$3|" /i /t "|" /f "text.txt" /o -
It looks for four consecutive hex digits, and inserts the slashes before the two pairs, and also removes all whitespace from each line. Once the file has been transformed, there are no more instances of four consecutive hex digits, nor is there any whitespace, so subsequent runs make no further changes.
Dave Benham
Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
Posted: 05 Apr 2019 09:44
by Aacini
... and you also should read the
very first post in this forum...
Code: Select all
@echo off
setlocal EnableDelayedExpansion
(for /F "delims=" %%a in (input.txt) do (
set "line="
for %%b in (%%a) do (
set "num=%%b"
set "line=!line!\x!num:~0,2!\x!num:~2!
)
echo !line!
)) > output.txt
move /Y output.txt input.txt
Antonio