bash 在 Ubuntu 上查找并删除 DOS 行结尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12508923/
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
Find and remove DOS line endings on Ubuntu
提问by exvance
I have found that many of my files have DOS line endings. In VI they look like this: "^M". I don't want to modify files that don't have these DOS line endings. How do I do this using a bash script? Thanks!
我发现我的许多文件都有 DOS 行结尾。在 VI 中,它们看起来像这样:“^M”。我不想修改没有这些 DOS 行结尾的文件。如何使用 bash 脚本执行此操作?谢谢!
EV
电动汽车
回答by nullrevolution
grep -URl ^M . | xargs fromdos
grep gets you a list of all files under the current directory that have DOS line endings.
grep 为您提供当前目录下具有 DOS 行结尾的所有文件的列表。
-Umakes grep consider line endings instead of stripping them away by default
-U使 grep 考虑行尾而不是默认将它们剥离
-Rmakes it recursive
-R使其递归
-lmakes it list only the filenames and not the matching lines
-l使它只列出文件名而不是匹配的行
then you're piping that list into the converter command (which is fromdoson ubuntu, dos2unixwhere i come from).
然后您将该列表传送到转换器命令中(fromdos在 ubuntu 上,dos2unix我来自哪里)。
NOTE:don't actually type ^M. instead, you'll need to press <Ctrl-V>then <Ctrl-M>to insert the ^Mcharacter and make grep understand what you're going for. or, you could type in $'\r'in place of ^M(but i think that may only work for bash...).
注意:实际上不要输入^M. 相反,您需要按<Ctrl-V>then<Ctrl-M>插入^M字符并使 grep 了解您要做什么。或者,您可以输入$'\r'代替^M(但我认为这可能仅适用于 bash ......)。
回答by Steve
One way using GNU coreutils:
一种使用方式GNU coreutils:
< file.txt tr -d '\r'
回答by Fredrik Pihl
On ubuntu, you use the fromdosutility
在 ubuntu 上,您使用该fromdos实用程序
fromdos test.txt
The above example would take a MS-DOS or Microsoft Windows file or other file with different line separators and format the file with new line separators to be read in Linux and Unix.
上面的示例将采用 MS-DOS 或 Microsoft Windows 文件或其他具有不同行分隔符的文件,并使用新行分隔符格式化文件以在 Linux 和 Unix 中读取。
回答by Guru
Many options are there..you can try with any of these.. http://www.theunixschool.com/2011/03/different-ways-to-delete-m-character-in.html
有很多选择..你可以尝试其中任何一个.. http://www.theunixschool.com/2011/03/different-ways-to-delete-m-character-in.html
回答by Thor
Note if you're converting multi-byte files you need to take extra care, and should probably try to use the correct iconv or recode from-encoding specifications.
请注意,如果您要转换多字节文件,则需要格外小心,并且可能应该尝试使用正确的 iconv 或从编码规范重新编码。
If it's a plain ASCII file, both of the below methods would work.
如果它是一个普通的 ASCII 文件,则以下两种方法都可以使用。
The flipprogram, in Debian the package is also called flip, can handle line-endings. From the manual:
该flip程序,在 Debian 中,该包也称为flip,可以处理行尾。从手册:
When asked to convert a file to the same format that it already
has, flip causes no change to the file. Thus to convert all
files to **IX format you can type
flip -u *
and all files will end up right, regardless of whether they were
in MS-DOS or in **IX format to begin with. This also works in the
opposite direction.
Or you could use GNU recode:
或者你可以使用 GNU 重新编码:
< /etc/passwd recode ..pc | tee a b > /dev/null
file a b
Output:
输出:
a: ASCII text, with CRLF line terminators
b: ASCII text, with CRLF line terminators
Convert to unix line-endings:
转换为 Unix 行尾:
recode pc.. a b
file a b
Output:
输出:
a: ASCII text
b: ASCII text
recode abbreviates dos line-endings as pc, so the logic with pc..is: convert from pc format to the default, which is latin1 with unix line-endings.
recode 将 dos 行尾缩写为pc,因此逻辑pc..是:从 pc 格式转换为默认值,即 latin1 和 unix 行尾。
回答by Vijay
you can use the command:
您可以使用以下命令:
dos2ux file.in>file.out or:
in perl:
在 perl 中:
perl -pi -e 's/\r//g' your_file
alternatively you can do:
或者你可以这样做:
- open in vi
- go to command mode
- type
:%s/[ctrl-V][CTRL-M]//g
- 在 vi 中打开
- 进入命令模式
- 类型
:%s/[ctrl-V][CTRL-M]//g
回答by Jarrod Juleff
A modification to the Winning answer if you need to filter by file ending
如果您需要按文件结尾进行过滤,请修改获胜答案
grep -URl ^M . | grep .php | xargs dos2unix
I used dos2unix instead of fromdos but the effect should be the same.
我用的是 dos2unix 而不是 fromdos 但效果应该是一样的。

