Windows批处理:如何保持空行与循环FOR / F [英] Windows Batch: How to keep empty lines with loop for /f

查看:1245
本文介绍了Windows批处理:如何保持空行与循环FOR / F的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找如何保持空行,当我浏览一个文件for循环。

I'm searching how to keep empty lines when I browse 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
)

其实它适用于每个人,但就我而言,这是行不通的。
例如,如果test1.txt的内容是:

Actually it works for everybody, but as far as I'm concerned it does not work. For instance if test1.txt content is :

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

在结果的test2.txt将是:

Result in test2.txt will be :

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

如果我一拖再拖的1傍明星*,结果是:

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

[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 

所需的输出是:

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

能否请你帮我解决这个麻烦?

Can you please help me to solve this trouble ?

非常感谢你。

推荐答案

这可以做到的。

@echo off
    setlocal enableextensions disabledelayedexpansion

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

从输出内找到指令就像

[123]texttexttext

在code使用右括号作为分隔符,所以令牌(我们要求两个标记: 1,* 1 * )的

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

但是,由于重复定界符作为仅一个定界符处理,如果一个行以闭括号开始它将被删除。这可能是prevented

But, as repeated delimiters are handled as only one delimiter, if one line begins with a closing bracket it will be removed. This can be prevented as

@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
    )

下面的数字作为分隔符和行标记化作为

Here the numbers are used as delimiters and the line is tokenized as

[   ]texttexttext
^   ^
%%a %%b

然后,第二令牌的值存储在变量中,与延迟扩展禁用的,以避免与数据内感叹词问题(将被处理/解析器取代,如果延迟扩展是活性)

Then the value of the second token is stored inside a variable, with delayed expansion disabled to avoid problems with exclamations inside the data (that will be handled/replaced by the parser if delayed expansion is active)

在的数据是可变内,延迟扩展被激活(需要的东西,因为我们要检索从code的块内改变一个变量的内容),以输出从第二位置的线(第一字在字符串为0)删除右括号。一旦完成,推迟扩张再次被禁用。

Once the data is inside the variable, delayed expansion is activated (something needed as we want to retrieve the contents from a variable changed inside a block of code) to output the line from the second position (first character in string is 0) to remove the closing bracket. Once done, delayed expansion is disabled again.

编辑作为OP已经将它整合到一个更大的/复杂的脚本,这code要面临的最常见的问题。

edited as the OP has to incorporate it to a larger/complex script, this code should face the most usual problems

@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
        )
    )

这篇关于Windows批处理:如何保持空行与循环FOR / F的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆