Windows package: how to save empty lines with a loop for / f

I am looking for how to keep blank lines when viewing a file with a for loop.

for /f "tokens=1* delims=[" %%i in ('type "test1.txt" ^| find /v /n ""') do (
SET tmp=%%i
echo !tmp! >> test2.txt
)

Actually it works for everyone, but as I understand it, it does not work. For example, if the contents of test1.txt:

Hello I come from France
I live in Paris

I'm sorry I don't know english, could we speak french please ?
If it doesn't bother you

Thank you

The result in test2.txt will be:

[1 
[2 
[3 
[4 
[5 
[6 
[7 

If I put "1" near the star "*", the result:

[1]Hello I come from France 
[2]I live in Paris 
[3] 
[4]I'm sorry I don't know english, could we speak french please ? 
[5]If it doesn't bother you 
[6] 
[7]Thank you 

Required Conclusion:

Hello I come from France
I live in Paris

I'm sorry I don't know english, could we speak french please ?
If it doesn't bother you

Thank you

Could you help me solve this problem?

Many thanks.

+4
source share
2 answers

It can be done as

@echo off
    setlocal enableextensions disabledelayedexpansion

    for /f "tokens=1,* delims=]" %%a in ('
        find /n /v "" ^< "file1.txt"
    ') do (
        >> "file2.txt" echo(%%b
    )

Exiting an internal command findis similar to

[123]texttexttext

The code uses a closing bracket as a separator, so tokens (we request two tokens: 1,*or 1*):

[123 texttexttext
^    ^
1    2
%%a  %%b

, , , .

@echo off
    setlocal enableextensions disabledelayedexpansion

    for /f "tokens=1,* delims=0123456789" %%a in ('
        find /n /v "" ^< "file1.txt"
    ') do (
        set "line=%%b"
        setlocal enabledelayedexpansion
        >>"file2.txt" echo(!line:~1!
        endlocal
    )

,

[   ]texttexttext
^   ^
%%a %%b

, ( / , )

, (- , , ), ( 0), . .

, OP / script,

@echo off
    rem For this test we will have delayed expansion from the start
    setlocal enableextensions enabledelayedexpansion

    rem External code block that will make delayed expansion necessary
    if 1==1 ( 
        rem Variables changed inside block
        set "input_file=file1.txt"
        set "output_file=file2.txt"

        rem Grab a reference to the content of the file variables
        for %%i in ("!input_file!") do for %%o in ("!output_file!") do (

            rem Prepare the environment for file work
            setlocal disabledelayedexpansion

            rem Prepare output file
            type nul > "%%~fo"

            rem Process input file and write to output file
            for /f "tokens=1,* delims=0123456789" %%a in ('
                find /n /v "" ^< "%%~fi"
            ') do (
                set "line=%%b"
                setlocal enabledelayedexpansion
                >>"%%~fo" echo(!line:~1!
                endlocal
            )

            rem Restore the previous environment
            endlocal
        )
    )
+6

, findstr find file2.txt , for/F for/F:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
>> "file2.txt" (
    for /F "delims=" %%a in ('findstr /N "^" "file1.txt"') do (
        set "line=%%a"
        setlocal EnableDelayedExpansion
        echo(!line:*:=!
        endlocal
    )
)
endlocal

findstr , :

1:Hello I come from France

!line:*:=! (- *) , .

>> > , .

0

Source: https://habr.com/ru/post/1649939/


All Articles