在 Ubuntu 上查找并删除 DOS 行结尾 [英] Find and remove DOS line endings on Ubuntu

查看:23
本文介绍了在 Ubuntu 上查找并删除 DOS 行结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我的许多文件都有 DOS 行结尾.在 VI 中,它们看起来像这样:^M".我不想修改没有这些 DOS 行结尾的文件.如何使用 bash 脚本执行此操作?谢谢!

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!

电动汽车

推荐答案

grep -URl ^M . | xargs fromdos

grep 为您提供当前目录下具有 DOS 行结尾的所有文件的列表.

grep gets you a list of all files under the current directory that have DOS line endings.

-U 使 grep 考虑行尾而不是默认将它们剥离

-U makes grep consider line endings instead of stripping them away by default

-R 使其递归

-l 使它只列出文件名而不列出匹配的行

-l makes it list only the filenames and not the matching lines

然后您将该列表传送到转换器命令中(在 ubuntu 上为 fromdosdos2unix 我来自哪里).

then you're piping that list into the converter command (which is fromdos on ubuntu, dos2unix where i come from).

注意:实际上不要输入 ^M.相反,您需要按 然后按 插入 ^M 字符并使grep 明白你要做什么.或者,您可以输入 $' ' 代替 ^M (但我认为这可能只适用于 bash ......).

NOTE: don't actually type ^M. instead, you'll need to press <Ctrl-V> then <Ctrl-M> to insert the ^M character and make grep understand what you're going for. or, you could type in $' ' in place of ^M (but i think that may only work for bash...).

这篇关于在 Ubuntu 上查找并删除 DOS 行结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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