编辑平面文件以在每行上包含特定数量的字符?

发布于 2024-11-19 09:21:02 字数 1037 浏览 3 评论 0原文

我想创建一个批处理文件,将平面文件中的每一行转换为特定数量的字符(在每行末尾添加空格)。

例子: 我有一个名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界等同你 2024-11-26 09:21:02

这是一种方法(我在“SET strTemp=%1”中的空格末尾留了一个句点。为了可见性,您可以将其删除:

@ECHO OFF

FOR /F %%a IN (text.txt) DO Call :FixString %%a
PAUSE
EXIT

:FixString
 SET strTemp=%1       .
 ECHO.%strTemp:~0,7% >> output.txt
 EXIT /b

/edit:

Powershell 适合您吗?处理回车会更容易在那里,像这样:

Foreach ($strLine in Get-Content c:\temp\text.txt) 
{ 
 $strTemp = $strLine + "........"
 If ($strTemp.Length -lt 1) {
  Add-Content c:\temp\output.txt $strTemp
 } Else {
  Add-Content c:\temp\output.txt $strTemp.SubString(0,7)
 }
}

如果每行中有空格,这个 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:

@ECHO OFF

FOR /F %%a IN (text.txt) DO Call :FixString %%a
PAUSE
EXIT

:FixString
 SET strTemp=%1       .
 ECHO.%strTemp:~0,7% >> output.txt
 EXIT /b

/edit:

Is Powershell an option for you? It would be easier to handle the carriage returns there, like this:

Foreach ($strLine in Get-Content c:\temp\text.txt) 
{ 
 $strTemp = $strLine + "........"
 If ($strTemp.Length -lt 1) {
  Add-Content c:\temp\output.txt $strTemp
 } Else {
  Add-Content c:\temp\output.txt $strTemp.SubString(0,7)
 }
}

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

梦在深巷 2024-11-26 09:21:02

FOR 循环确实省略了空行。这是设计使然。要解决这个问题,您可以使用以下命令:

FIND /N /V "" < sourcefile

认为没有行可以匹配 "" (空字符串),因此只需 FIND "" < file 将产生空输出。但 /V 选项会导致输出反转:FIND 将输出那些匹配搜索字符串的行,而不是输出那些< em>不匹配。所以 FIND /V "" < file本质上会导致FIND输出file所有行。 /N 使每一行前面都加上行号,如下所示:

[number]text
[number]text

因此,空行将如下所示:

[number]
[number]
…

因此,现在我们可以迭代所有行。我们只需要删除所有 [number] 部分,然后使用 @Dave 的想法附加空格并剪掉前 7 个字符。以下是完整的脚本:(

@ECHO OFF
FOR /F "delims=" %%L IN ('FIND /N /V "" ^<text.txt') DO CALL :process "%%L"
GOTO :EOF

:process
SET "line=%~1"
SET "line=%line:*]=%       "
ECHO(%line:~0,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:

FIND /N /V "" < sourcefile

It is considered that no line can match "" (empty string), so simply FIND "" < 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. So FIND /V "" < file essentially causes FIND to output all the lines of file. And /N causes every line to be prepended with the line number, like this:

[number]text
[number]text

Accordingly, empty lines will look just like this:

[number]
[number]
…

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:

@ECHO OFF
FOR /F "delims=" %%L IN ('FIND /N /V "" ^<text.txt') DO CALL :process "%%L"
GOTO :EOF

:process
SET "line=%~1"
SET "line=%line:*]=%       "
ECHO(%line:~0,7%#

(The # character is only added for visual indication.)

UPDATE

Added ( just after ECHO. This solves the ECHO 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 the FOR loop after /F to account for spaces in the text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文