批处理文件+转换LF到CR + LF [英] batch file + convert LF to CR+LF

查看:406
本文介绍了批处理文件+转换LF到CR + LF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个名为shell脚本文件 LineFeed.sh 这不转换的功能的换行( LF )以回车+换行。我们希望同样通过在Windows中的批处理文件来完成。这可能吗?

Linux的shell文件

E_WRONGARGS = 65
猫OutputList |阅读时-r线

如果[-z$线]
然后
回声用法:`$基名文件名0`到转换
退出$ E_WRONGARGS
科幻
NEWFILENAME = $ Line.unx
CR ='\\ 015'#回车。
       #015是八进制的ASCII code为CR。
       #线路中CR-LF一个DOS文本文件末尾。
       在UNIX文本文件#行结束仅LF。
TR -d $为CR $ 1 GT; $ NEWFILENAME //这里的删除CR,但我需要追加LF
#删除CR的,并写入新文件。
DONE
回声原始的DOS文本文件是\\$ 1 \\。
回声转换UNIX文本文件是\\$ NEWFILENAME \\。
退出0


解决方案

您可以找到这个维基百科页面

  TYPE unix_file | FIND/ V> dos_file

请记住,你不能将输出重定向到你读取相同的文件。这适用于pretty太多的所有系统和贝壳,所以,一个附加的重命名是必要的。

这里的关键是,键入知道如何阅读LF行结束而找到然后将它们转换做CRLF 。 键入不会单独与输出(它应该的,因为具有简单的转储文件的内容与他们搞乱命令不好:-))做任何事情。

We have a shell script file named LineFeed.sh which does a function of converting a Linefeed(LF) to Carriage Return + LineFeed. We want the same to be done by a batch file in windows . Is it possible?

Linux shell file

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

解决方案

You can find one way on this Wikipedia page:

TYPE unix_file | FIND "" /V > dos_file

Remember that you can't redirect the output to the same file you're reading from. This applies to pretty much all systems and shells, so an additional rename is necessary.

The key here is that type knows how to read LF line endings and find will then convert them do CRLF. type alone won't do anything with the output (it's supposed to, because having a command that simply dumps the file contents messing with them isn't good :-)).

这篇关于批处理文件+转换LF到CR + LF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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