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

发布于 2024-09-07 09:08:37 字数 775 浏览 7 评论 0原文

我们有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戈亓 2024-09-14 09:08:37

您可以在此维基百科页面上找到一种方法:

TYPE unix_file | FIND "" /V > dos_file

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

这里的关键是 type 知道如何读取 LF 行结尾,然后 find 会将它们转换为 CRLF。 type 本身不会对输出执行任何操作(应该如此,因为使用一个简单地转储文件内容的命令并不好:-))。

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 :-)).

枫林﹌晚霞¤ 2024-09-14 09:08:37

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

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

:一般想法是替换原始文件,然后您可以将文件移回临时位置并删除临时文件夹

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:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文