如何禁用/F的eol和delims选项? [英] How to disable both `eol` and `delims` options of `for /F`?

查看:117
本文介绍了如何禁用/F的eol和delims选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

带有/F开关的for命令用于解析文本字符串行(文字字符串,从文本文件读取或从命令行输出中检索),并将每个文本字符串解析为一个或多个标记.

如何在不替换任何字符或忽略任何行的情况下原样获得一行文本?

我知道for /F "delims=" %L in (*) do echo."%L"会返回未编辑的每个已分析(非空)行.但是,由于eol选项默认为;,因此以该字符开头的每一行都将被忽略.
如果我使用for /F "tokens=* eol=" %L in (*) do echo."%L",则我禁用了eol选项 ["此说法不正确,"eol="不会禁用eol选项,但将"定义为eol字符!] ,但是delims选项默认为空格和制表符,因此所有前导空格和/或制表符都将被删除.
(在批处理文件中使用%%L.在这里*代表文本字符串的任何有效来源.)

所以我的问题是:是否可以指定 delims eol个字符?

我试图指定两个选项字符串("eol=" "delims="),但是这样会导致语法错误.选项字符串"eol=delims="也是如此.

注意:当标记化时,即设置delims选项时,此问题不会持续存在,因为它似乎被应用了比eol更高的优先级(奇怪的是,幸运的是, ),因此您可以通过将delims字符也指定为eol来隐藏" delims后面的eol.

解决方案

1),您将永远无法强制蝙蝠不命令并使用"tokens=1* delims=:"这样的选项来解决(排序),并仅获取第二个标记

2)要同时停用 eol和delim ,您可以使用以下语法:

For /f tokens^=*^ delims^=^ eol^= %%a in (file.txt) do echo.%%a

尽管空行仍然会被忽略.

The for command with the /F switch is used to parse lines of text strings (literal strings, read from text files, or retrieved from command line output) and to parse each to one or more tokens.

How can I get a line of text as it is, without any characters replaced nor any lines ignored?

I know that for /F "delims=" %L in (*) do echo."%L" returns each parsed (non-empty) line unedited. However, since the eol option defaults to ;, every line starting with that character will be ignored.
If I use for /F "tokens=* eol=" %L in (*) do echo."%L", I disable the eol option [Edit: This claim is not true, "eol=" does not disable the eol option, but it defines " as the eol character!], but the delims option defaults to space and tab, so any leading spaces and/or tabs become removed.
(Use %%L within batch files. The * stands for any valid source of text string here.)

So my question in other words: is there a way to specify no delims and no eol characters?

I tried to specify two option strings ("eol=" "delims=") but such results in a syntax error. So does option string "eol=delims=".

Note: This problem does not persist when tokenizing, that is, when the delims option is set, because that seems to be applied with a higher priority than eol (strangely but luckily), so you can "hide" eol behind delims by specifying a delims character also as eol.

解决方案

1) you will be never able to force bat to not ignore empty / delimiter-only lines.This can be (sort-of) worked around by piping to findstr /R /N "^" command and use options like "tokens=1* delims=:" and get only the second token

2) To deactivate eol and delim at the same time you can use this syntax:

For /f tokens^=*^ delims^=^ eol^= %%a in (file.txt) do echo.%%a

though the empty lines still will be ignored.

这篇关于如何禁用/F的eol和delims选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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