编辑平面文件以在每行上包含特定数量的字符?
我想创建一个批处理文件,将平面文件中的每一行转换为特定数量的字符(在每行末尾添加空格)。
例子: 我有一个名为“text.txt”的文本文件,它看起来像这样:
1
22
333
4444
55555
我想在其上运行一个批处理文件并收到以下内容(所有行都是 7 个字符长):(
1......
22.....
333....
4444...
55555..
我必须用点替换空格使其可见)
这可能吗?如何实现?
更新:
酷,效果完美。
是否也可以将一个空行(仅 CRLF)转换为 7 个空格?当我在包含空行的文件上运行上述内容时,它们将被删除。
谢谢!
编辑:只是为了澄清一下,这是文件:
1
22
333
22
1
4444
55555
我想收到:
1......
22.....
.......
333....
22.....
.......
1......
4444...
55555..
再次感谢!
更新 2:
Andriy 的回答让我向前迈出了几步。不过我有一些问题。如果我保留 # 符号,输出将是这样的(仍然是句点而不是空格):
1.......
22......
ECHO is off.
333.....
22......
ECHO is off.
1.......
4444....
55555...
如果一行以空格开头,它将输出为:
ECHO is off.
Powershell is不幸的是不是一个选项...
这可能是一个更好的 选择例如,
text.txt:
This is
the input
file
I
want to convert
当然现在我们必须使记录更长,比如说20个字符。
I would like to create a batch file that converts every line in a flat file to e specific amount of characters (add spaces at the end of every row).
Example:
I have a text file called "text.txt", and it looks like this:
1
22
333
4444
55555
I would like to run a batch file on it and recieve the following (all lines are 7 characters long):
1......
22.....
333....
4444...
55555..
(I had to replace my spaces with dots to make it visible)
Is this possible, and how?
Update:
Cool, that works perfect.
Is it also possible to convert a blank line (just CRLF) to 7 spaces? When I run the above on a file with empty lines they are deleted.
Thanks!
EDIT: Just to clarify, this is the file:
1
22
333
22
1
4444
55555
And I want to recieve:
1......
22.....
.......
333....
22.....
.......
1......
4444...
55555..
Thanks again!
Update 2:
Andriys answer is getting me a few steps forward. I've got a few issues though. If I leave the # sign out, the output will be like this (still periods instead of spaces):
1.......
22......
ECHO is off.
333.....
22......
ECHO is off.
1.......
4444....
55555...
And if a line starts with a space it will be outputted as:
ECHO is off.
Powershell is unfortunately not an option...
This might be a better example,
text.txt:
This is
the input
file
I
want to convert
Of course now we have to make the records longer, let's say 20 char.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种方法(我在“SET strTemp=%1”中的空格末尾留了一个句点。为了可见性,您可以将其删除:
/edit:
Powershell 适合您吗?处理回车会更容易在那里,像这样:
如果每行中有空格,这个 Powershell 脚本也将表现出来,我之前给你的 .cmd 脚本将遇到问题(你需要更改 For 命令的 delims= 参数来处理它们)。
Here's one way (I left a period at the end of the spaces in "SET strTemp=%1 ." for visibility, you can remove it:
/edit:
Is Powershell an option for you? It would be easier to handle the carriage returns there, like this:
This Powershell script will also behave if there are spaces within each line, the .cmd script I gave you earlier will have trouble with them (you'd need to change the delims= parameter of the For command to handle them).
FOR
循环确实省略了空行。这是设计使然。要解决这个问题,您可以使用以下命令:认为没有行可以匹配
""
(空字符串),因此只需FIND "" < file
将产生空输出。但/V
选项会导致输出反转:FIND
将输出那些匹配搜索字符串的行,而不是输出那些< em>不匹配。所以 FIND /V "" < file本质上会导致FIND
输出file
的所有行。/N
使每一行前面都加上行号,如下所示:因此,空行将如下所示:
因此,现在我们可以迭代所有行。我们只需要删除所有
[number]
部分,然后使用 @Dave 的想法附加空格并剪掉前 7 个字符。以下是完整的脚本:(添加
#
字符仅用于视觉指示。)UPDATE
在
ECHO
( >。基本上,您可以使用许多不同的字符来代替(
,例如.
或 <例如,code>, 但是,如 这个答案,(
似乎最可靠。更新 2
添加了
"delims="
选项FOR
在/F
之后循环以解决文本中的空格。The
FOR
loop does omit empty lines. This is by design. To work around that, you could use the following command:It is considered that no line can match
""
(empty string), so simplyFIND "" < file
would produce empty output. But the/V
option causes the inversion of the output: instead of the lines that match the search string,FIND
is to output those that do not match it. SoFIND /V "" < file
essentially causesFIND
to output all the lines offile
. And/N
causes every line to be prepended with the line number, like this:Accordingly, empty lines will look just like this:
So, now we are able to iterate over all the lines. We only need to remove all the
[number]
parts, then use @Dave's idea of appending the spaces and cutting out the first 7 characters. Here's a full script:(The
#
character is only added for visual indication.)UPDATE
Added
(
just afterECHO
. This solves theECHO is off
issue. Basically, you can use a number of different characters instead of(
, like.
, or,
for instance. But, as shown in this answer,(
seems most reliable.UPDATE 2
Added the
"delims="
option to theFOR
loop after/F
to account for spaces in the text.