批处理文件 +将 LF 转换为 CR+LF
我们有一个名为 LineFeed.sh
的 shell 脚本文件,它的功能是将换行符 (LF
) 转换为回车+换行符。我们希望通过 Windows 中的批处理文件来完成相同的操作。是否可以?
Linux 外壳文件
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在此维基百科页面上找到一种方法:
请记住,您无法重定向输出到您正在读取的同一文件。这几乎适用于所有系统和 shell,因此需要进行额外的重命名。
这里的关键是
type
知道如何读取 LF 行结尾,然后find
会将它们转换为 CRLF。type
本身不会对输出执行任何操作(应该如此,因为使用一个简单地转储文件内容的命令并不好:-))。You can find one way on this Wikipedia page:
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 andfind
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 :-)).基于使用
type
命令执行此操作的普遍注意的方法,您还可以转换所有文件(或您可能喜欢的任何通配符)并使用以下命令将它们转储到临时文件夹中:一般想法是替换原始文件,然后您可以将文件移回临时位置并删除临时文件夹
Building on the generally noted way of doing this using the
type
command, you can also convert all of the files (or whatever wildcard you may prefer) and dump them in a temp folder using the following:If the general idea was to replace the originals then you can just move the files back out of the temporary location and delete the temp folder