1.

Solve : Extracting from a Text file using FOR /f?

Answer»

Need some advice -

Trying to use the FOR /f command to extract specific data from a text file and into an output file.

Looking to do the following:
00024803830002480383002480383

Need bytes 5-10 (480383) and dump to another text file.
And then add to the beginning and end of that data

So ultimately:  test480383test

Using this so far: 

FOR /f "tokens=1,2 delims=0002" %%a in (test.txt) do echo %%a %%b

But this only gives me:  48 383

Kinda new to this command so looking for a little help - really appreciate it.
Thanks
It is not clear what you want.
Give more examples.
00024803830002480383002480383

The above set of numbers are contained w/in a text file (and there are 100's of lines like this with different values)

I want to extract bytes 5-10 (which in the above EXAMPLE would be 480383) and place into a separate text file as:

test480383test

test.txt
00024803830002480383002480383
00022621170002480383002480383
00056432510002480383002480383
00069137560002480383002480383

Example batch
echo off
setlocal enabledelayedexpansion
for /f "delims=" %%A in (test.txt) do (
    set inputline=%%A
    set sliceline=!inputline:~4,6!
    echo INPUT  !inputline!
    echo Output test!sliceline!test
    )

Batch output
Input  00024803830002480383002480383
Output test480383test
Input  00022621170002480383002480383
Output test262117test
Input  00056432510002480383002480383
Output test643251test
Input  00069137560002480383002480383
Output test913756test


This is a robust option

Code: [Select]type file.txt |repl "^....(......).*" "test$1test" >newfile.txt
This uses a helper batch file called `repl.bat` (by dbenham) - download from:  https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.
Salmon Trout -

Thanks - that's exactly what I needed.  Made a few edits:

echo off
setlocal enabledelayedexpansion
for /f "delims=" %%A in (test.txt) do (
    set inputline=%%A
    set sliceline=!inputline:~4,6!
    echo test!sliceline!test
    ) >>log.log


Is there a specific command to obtain just the data that STARTS at a specific column/byte?

example:

11111 22222 33333
111 222 333 444

Both '33333' and '444' start on byte/column 13

=-=-=-=
Just figured this out...while I was saving this postThis is a post script.
If you know ahead of time that you have to parse file NAMES, it helps to adopt some conventions for file naming. The underline char _ is allowed both in Windows and some other systems and can be used om  the internet. That could be used to show where a key begins or ends. Otherwise, you have to make things position dependent.  In on e example it was ALWAYS chars 5 to 10 inclusive. However, the use of delimiters makes it easier to avoid human errors when naming files.




Discussion

No Comment Found