windows 如何在单个文件中用 lf 替换 crlf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27810758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to replace crlf with lf in a single file
提问by Charles Belov
How do I convert a single file that has crlf line returns to have lf line returns?
如何将具有 crlf 行返回的单个文件转换为具有 lf 行返回?
git is already correctly handling crlf to lf conversion automatically for files when I push them to a remote repository, but in this particular case I am not doing a push to a repository. Rather, I am uploading a file, using a file field on an HTML form, to a website that requires that the file have lf line returns. So I need to be able to convert this file individually.
当我将文件推送到远程存储库时,git 已经正确处理了文件的 crlf 到 lf 转换,但在这种特殊情况下,我没有推送到存储库。相反,我使用 HTML 表单上的文件字段将文件上传到要求文件具有 lf 行返回的网站。所以我需要能够单独转换这个文件。
My available potentially useful tools available on this computer would be git and Dreamweaver CC 2014.1. (I'm guessing Word, Wordpad and Notepad are not viable options but I'm open to being corrected.)
我在这台计算机上可用的潜在有用工具是 git 和 Dreamweaver CC 2014.1。(我猜 Word、写字板和记事本不是可行的选择,但我愿意接受纠正。)
I am on Windows 7 and using git line commands.
我在 Windows 7 上使用 git line 命令。
回答by René Link
The git installation on windows usually includes the dos2unix
tool.
Windows 上的 git 安装通常包含该dos2unix
工具。
dos2unix <file>
But in your case you should use .gitattributes
to prevent the file from being converted on windows.
但是在您的情况下,您应该使用.gitattributes
来防止文件在 Windows 上被转换。
A .gitattributes
file can look like this
一个.gitattributes
文件可以是这样的
*.vcproj eol=crlf
*.sh eol=lf
From the .gitattributes documentation
Set to string value "lf"
This setting forces Git to normalize line endings to LF on checkin and prevents conversion to CRLF when the file is checked out.
设置为字符串值“lf”
此设置强制 Git 在签入时将行尾规范化为 LF,并防止在签出文件时转换为 CRLF。
Just commit the .gitattributes
file and your file will be checkout out on every system with LF
line ending.
只需提交.gitattributes
文件,您的文件将在每个系统上以LF
行结尾签出。
回答by vijking
To convert from Windows-style text to Unix-style text:
要将 Windows 样式的文本转换为 Unix 样式的文本:
perl -pe 's/\r$//g' < windows_file.txt > unix_file.txt
To convert from Unix-style text to Windows-style text:
要将 Unix 样式的文本转换为 Windows 样式的文本:
perl -pe 's/(?<!\r)\n/\r\n/g' < unix_file.txt > windows_file.txt
This "Unix-to-Windows" command is "safe", by which I mean that it won't add extra Carriage-Returns to a file that is already in Windows format. I hope that someone will get some use out of it.
这个“Unix-to-Windows”命令是“安全的”,我的意思是它不会向已经是 Windows 格式的文件添加额外的回车。我希望有人能从中得到一些好处。
回答by MalliKharjuna Rao
回答by Ankur.Sharma
For a single file you can use Notepad++ replace utility:
对于单个文件,您可以使用 Notepad++ 替换实用程序:
- Go to Search -> Replace (or ctrl-h)
- In the dialog box select "Extended" search mode
- Set "Find What" to:
\r\n
- Set "Replace With" to:
\n
- Click "Replace All"
- 转到搜索 -> 替换(或 ctrl-h)
- 在对话框中选择“扩展”搜索模式
- 将“查找内容”设置为:
\r\n
- 将“替换为”设置为:
\n
- 点击“全部替换”
回答by Abdelouahab
Word and wordpad are to avoid! NEVERtry to modify some code with it, they will add extra code for the file.
Word 和写字板是要避免的!永远不要尝试用它修改一些代码,他们会为文件添加额外的代码。
To convert the line end, you just use Notepad++or Scite, then you choose your end of file: Windows, Mac, Linux.
要转换行尾,您只需使用 Notepad++或Scite,然后选择文件的结尾:Windows、Mac、Linux。
Here is an example using Scite:
这是使用 Scite 的示例:
回答by Canonne Gregory
inside your root folder, create a file '.gitattributes' and add this line :
在您的根文件夹中,创建一个文件“.gitattributes”并添加以下行:
- eol=lf
- eol=lf
save the folder and :
保存文件夹并:
git add . ... all files of your project will LF
git 添加。...您项目的所有文件都将 LF