保留行尾 [英] Preserve line endings

查看:41
本文介绍了保留行尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 上运行 sed 进行一些替换,我注意到它会自动将行结尾转换为 Unix (\n).是否可以选择告诉 sed 使用 Windows 行尾(\r\n),或者甚至更好地保留文件中的行尾?

注意:我使用 unxutils 中的 sed:http://unxutils.sourceforge.net/

解决方案

您可以使用 -b 选项让 sed 将文件视为二进制文件.这将解决 Windows 上 cygwin 的 sed 问题.

示例:sed -b 's/foo/bar/'

如果你想匹配行尾,记得匹配、捕获和复制可选的回车.

示例:sed -b 's/foo\(\r\?\)$/bar\1/'

来自 sed 手册页:<块引用>

-b --二进制

此选项在每个平台上都可用,但仅在操作系统区分文本文件和二进制文件时才有效.当进行这样的区分时——如 MS-DOS、Windows、Cygwin 的情况——文本文件由由回车符和换行符分隔的行组成,并且 sed 看不到结尾的 CR.当指定此选项时,sed 将以二进制模式打开输入文件,因此不需要这种特殊处理并考虑行以换行结束.`

I run sed to do some substitution on windows and I noticed that it automatically converts line endings to Unix (\n). Is there an option to tell sed to use Windows line endings (\r\n) or even better to preserve the line endings from the file?

Note: I use sed from unxutils: http://unxutils.sourceforge.net/

解决方案

You can use the -b option for sed to have it treat the file as binary. This will fix the problem with cygwin's sed on Windows.

Example: sed -b 's/foo/bar/'

If you wish to match the end of the line, remember to match, capture and copy the optional carriage return.

Example: sed -b 's/foo\(\r\?\)$/bar\1/'

From the sed man page:

-b      --binary

This option is available on every platform, but is only effective where the operating system makes a distinction between text files and binary files. When such a distinction is made—as is the case for MS-DOS, Windows, Cygwin—text files are composed of lines separated by a carriage return and a line feed character, and sed does not see the ending CR. When this option is specified, sed will open input files in binary mode, thus not requesting this special processing and considering lines to end at a line feed.`

这篇关于保留行尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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