批处理文件+将LF转换为CR + LF

问题描述 投票:5回答:2

我们有一个名为LineFeed.sh的shell脚本文件,它具有将换行符(LF)转换为回车符+ LineFeed的功能。我们希望在Windows中通过批处理文件完成相同的操作。可能吗?

Linux shell文件

E_WRONGARGS=65
cat OutputList|while read -r Line 
do 
if [ -z "$Line" ]
then
echo "Usage: `basename $0` filename-to-convert"
exit $E_WRONGARGS
fi
NEWFILENAME=$Line.unx
CR='\015'  # Carriage return.
       # 015 is octal ASCII code for CR.
       # Lines in a DOS text file end in CR-LF.
       # Lines in a UNIX text file end in LF only.
tr -d $CR < $1 > $NEWFILENAME // here its deleting CR but i need to append LF
# Delete CR's and write to new file.
done
echo "Original DOS text file is \"$1\"."
echo "Converted UNIX text file is \"$NEWFILENAME\"."
exit 0
batch-file batch-processing
2个回答
13
投票

你可以在this Wikipedia page上找到一种方法:

TYPE unix_file | FIND "" /V > dos_file

请记住,您无法将输出重定向到您正在读取的同一文件。这几乎适用于所有系统和shell,因此需要额外的重命名。

这里的关键是type知道如何读取LF行结尾,然后find将它们转换为CRLF。单独的type不会对输出做任何事情(它应该是,因为有一个命令只是转储文件内容弄乱它们并不好:-))。


0
投票

基于通常使用type命令执行此操作的方法,您还可以转换所有文件(或您喜欢的任何通配符)并使用以下内容将它们转储到临时文件夹中:

md temp
for %a in (*.*) do type "%a" | find /v "" > temp\"%a"

如果一般的想法是替换原件,那么您只需将文件移回临时位置并删除临时文件夹即可

© www.soinside.com 2019 - 2024. All rights reserved.