如何修复文本文件批处理输出中的换行符? [英] How to fix line break in text file batch output?

查看:74
本文介绍了如何修复文本文件批处理输出中的换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用批处理合并两个文本文件数据.

I'm trying to merge two text file data using batch.

这是将两个文本文件转换为单个文本文件.

This is to make two text files to single text file.

我有两个文本文件,

file1.txt 包含

apple

file2.txt 包含

ball

我正在使用批处理脚本,

Am using the batch script,

type file1.txt file2.txt > output.txt

我希望 output.txt 中的输出为

apple
ball

但是, output.txt 中的实际输出是

appleball

输出中似乎缺少换行符.

Seems like line break is missing in output.

推荐答案

在Windows中合并文件实际上是使用 copy 命令

Merging files in windows is actually done using the copy command

 Copy file1.txt+file2.txt output.txt

如果所需的源文本文件是源目录中的唯一文件,或者如果您可以使用标准cmd通配符 * ?,您可以一次将无限数量的文件合并在一起:

If the source text files you want are the only ones in the source directory, or if you can easily ID just the ones you want using a file glob using the standard cmd wildcards * or ?, you can merge an unlimited number of files together in one go:

 Copy File?.txt output.txt

 Copy File*.txt output.txt

(大声笑,我丢失了原始编辑,但请重试)

(Lol, grr I lost the original edit, but,lets try again )

您的源文件中也可能只包含换行(LF).我相信复制应该合并这些就好了.

Its also possible that you only have Line Feeds (LF)s in your source files. I believe Copy should merge these just fine.

Windows仍然优先使用LF(CRLF)之前的回车(CR),但是记事本和其他一些程序将显示LF文件,就好像它们具有CRLF一样.

Windows still prefers Carriage Returns (CR)s before the LFs, (CRLF) but notepad and some other programs will display the LF files as if they had CRLFs

但是,命令行上的许多命令不能正确显示此信息,尤其是在输出的最后一行上,在这种情况下,它们经常将以下文本连接到该文本上.

However many commands at the command line don't display this info correctly, especially on the Last line of output, where they will often concatenate the following text to it in scenarios like this.

要解决此问题,您可以将 More Type 结合使用以强制CRLF.

To get around this you can use More with Type to force CRLFs.

( TYPE File1.txt | MORE & TYPE File2.txt | MORE /P) > Output.txt

我有时间进行测试,最初的假设是您不能将 type & 一起使用,或者将type与通配符 type 到 more 是正确的,因此,在确认后,我删除了该位.

I had time to test and my original supposition that you couldn't & the types together or use type with a wildcard type to a more was right, so I removed that bit now that it's confirmed.

因此,为简化起见,您可以使用一个简单的循环,但是如asch所述,这会将您的行数限制为8个字符.

So to simplify you could use a simple loop, but as asch notes this limits your lines to 8 characters.

 for %A IN ( "Y:\t\A\file*.txt" ) DO @( type "%A" | more /P )

带有通配符的

more 将在文件之间进行分页,即使在cmd脚本中也是如此,因此尽管您可以执行与上述相同的操作,但更多的"file * .txt"还是不存在:

And more with a wildcard will put the pagination between the files, even in a cmd script so more "file*.txt" is out although you could do the same scenario as above:

 for %A IN ( "Y:\t\A\file*.txt" ) DO @( More /P "%A" )

但这有其局限性.

那么,是否想要一些在行长或总行数上没有这些字符限制的东西?

好吧,如果我们可以编辑源文件,那么只需在回显末尾添加新行即可.

Well if we may edit the source files, then simply echoing a new line append to the will do the needful.

 for %A IN ( "Y:\t\A\file*.txt" ) DO @( ECHO(>"%A" )

完成后,我们可以使用没有行数或长度限制的复制合并方法:

Once that is done we can use the copy merge method which will have no line or length limits:

 Copy "Y:\t\A\file*.txt" output.txt

无法编辑原始文件吗?将它们复制到临时文件中以执行需要的操作,然后在完成后删除该临时目录.

Can't edit the original files? Copy them to temp files to do the needful, then delete that temp directory when done.

 MD "%Temp%\filedir"
 Copy  "Y:\t\A\file*.txt" "%Temp%\filedir\*"
 FOR %A IN ( "%Temp%\filedir\*" ) DO @( ECHO(>"%~A" )
 Copy "%Temp%\filedir\*" output.txt
 RD /S /Q "%Temp%\filedir"

这篇关于如何修复文本文件批处理输出中的换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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