Question: For example I have a text file `test.txt` that contains like this:

> 
> 
> ```
> Item1 $23 Item2 $24
> Item3 $25
> 
> ```
> 
> 

When I run this script:

```
@echo off
for /f "delims=$ tokens=1*" %%A in (test.txt) do echo %%B >> result.txt

```

The result is:

> 
> 
> ```
> 23 Item2 $24
> 25
> 
> ```
> 
> 

I want the result is:

> 
> 
> ```
> 23
> 24
> 25
> 
> ```
> 
> 

How should I repair the script?

Answer: 
Try the following script (see the explanatory `rem` remarks in the code):

```cmd
@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_IFILE=test.txt"
set "_OFILE=result.txt"

rem // Store line-break in variable:
(set ^"LF=^
%= blank line =%
^")

rem // Write to resulting file:
> "%_OFILE%" (
    rem // Loop through lines of text file:
    for /F "usebackq delims=" %%L in ("%_IFILE%") do (
        rem // Store current line in variable:
        set "LINE=%%L"
        rem // Toggle delayed expansion to be able to write and read variable in same code block:
        setlocal EnableDelayedExpansion
        rem // Replace each `$` by line-break and loop over resulting lines skipping the first one:
        for /F "skip=1" %%K in (^"!LINE:$^=^%LF%%LF%!^") do (
            rem // Return first token of each line:
            echo(%%K
        )
        endlocal
    )
)

endlocal
exit /B

```

At first, this replaces every `$` by a line-break, so the first line of `test.txt` becomes:

> 
> 
> ```cmd
> Item1 
> 23 Item2 
> 24
> 
> ```
> 
> 

And the second one becomes:

> 
> 
> ```cmd
> Item3
> 25
> 
> ```
> 
> 

Then it extracts the first `SPACE`- or `TAB`-separated token from each of the newly formed multi-line strings, skipping the first line, so only the numbers are returned.
This should do exactly what you want.

```
@echo off
setlocal enabledelayedexpansion
(for /f "usebackq tokens=* delims= " %%i in ("test.txt") do (
    set "_br=%%i"
    echo(!_br: =^

!
  )
)>_tmp
(for /f "delims=$ tokens=*" %%a in ('type _tmp ^| find "$"') do echo %%a)>result.txt

```

**Note** that you must copy the script as is. The extract of the script as show below does the replacement of whitespace with new line and therefore cannot change at all, the position and the extra line must remain as is for this to work as intended.

```
    echo(!_br: =^

!

```

This script just splits each word separated by a space and redirects it to a new file, we then specifically search for the words containing `$` and redirect those hits only.