如何使用批处理从文本文件中删除回车符和换行符? [英] How to remove carriage return and line feed characters from a text file using batch?

查看:807
本文介绍了如何使用批处理从文本文件中删除回车符和换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个固定宽度的文本文件,因此它包含前导零和空格,我需要从文件中删除回车符和换行符. 您能告诉我如何使用批处理脚本吗?

I've a fixed width text file so it contains leading zeros and spaces and I need to remove carriage return and line feed characters from the file. Could you please let me know how can I do this using batch script?

输入:

ABCDEF  GHIJK0000ADS
ABCDEF  GHIJK0000ADS
ABCDEF  GHIJK0000ADS

输出:

ABCDEF  GHIJK0000ADSABCDEF  GHIJK0000ADSABCDEF  GHIJK0000ADS

谢谢, 尼兰詹

推荐答案

如果您现有的行可能以空格开头,则没有简单的纯批处理解决方案. 可以在不使用换行符的情况下编写此类行,但是需要大量代码.

There is no trivial pure batch solution if you have existing lines that may begin with spaces. It is possible to write such lines without newlines, but it takes a lot of code.

还有其他一些问题可能会使纯批处理解决方案更加复杂.

There are other issues that can further complicate a pure batch solution.

通常,如果您想要一个健壮的通用解决方案,则Windows批处理是处理文本文件的不佳选择,

In general, Windows batch is a poor choice for manipulating text files if you want a robust, general purpose solution,

这就是为什么我写了 JREPL.BAT-正则表达式文本处理实用程序的原因. JREPL是纯脚本(混合批处理/JScript),可从XP开始在任何Windows计算机上本地运行.不需要第三方exe文件.

That is why I wrote JREPL.BAT - a regular expression text processing utility. JREPL is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward. No 3rd party exe file is required.

通过jrepl /?jrepl /??可从命令控制台访问完整的文档,以进行分页输出.

Full documentation is accessed from the command console via jrepl /?, or jrepl /?? for paged output.

该解决方案对于JREPL来说是微不足道的.

The solution is downright trivial with JREPL.

call jrepl "[\r\n]" "" /m /f "input.txt" /o "output.txt"

如果要覆盖原始文件,则

If you want to overwrite the original file, then

call jrepl "[\r\n]" "" /m /f "input.txt" /o -

只要JScript可以将您的整个文件读入内存,此解决方案就可以工作.我认为该限制接近1 GB.

This solution will work as long as your entire file can be read into memory by JScript. I believe the limit is close to 1 gigabyte.

从2020-02-29发布的JREPL版本8.5开始,大小限制已被消除.早期版本需要/M选项才能将整个文件加载到内存中. 8.5版引入了/EOL选项,该选项指定写入每行时要使用的行序列的结尾.可以将该值设置为空字符串,从而删除所有回车符和换行符,并通过一次处理一行来做到这一点.

The size limit has been eliminated starting with JREPL version 8.5 that was released 2020-02-29. Prior versions required the /M option to load the entire file into memory. Version 8.5 introduces the /EOL option that specifies the end of line sequence to be used when writing each line. The value can be set to an empty string, thus removing all carriage returns and line feeds, and it does this by processing one line at a time.

call jrepl "^" "" /eol "" /f "input.txt" /o "output.txt"

这篇关于如何使用批处理从文本文件中删除回车符和换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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