如何在 Linux 上使用 grep 搜索包含 DOS 行尾 (CRLF) 的文件? [英] How do you search for files containing DOS line endings (CRLF) with grep on Linux?

查看:37
本文介绍了如何在 Linux 上使用 grep 搜索包含 DOS 行尾 (CRLF) 的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Linux 上用 grep 搜索包含 DOS 行结尾的文件.像这样:

I want to search for files containing DOS line endings with grep on Linux. Something like this:

grep -IUr --color '
' .

以上似乎与文字 rn 匹配,这不是我们想要的.

The above seems to match for literal rn which is not what is desired.

它的输出将通过 xargs 传送到 todos 中,像这样将 crlf 转换为 lf

The output of this will be piped through xargs into todos to convert crlf to lf like this

grep -IUrl --color '^M' . | xargs -ifile fromdos 'file'

推荐答案

Use Ctrl+V, Ctrl+M 在您的 grep 字符串中输入文字回车符.所以:

Use Ctrl+V, Ctrl+M to enter a literal Carriage Return character into your grep string. So:

grep -IUr --color "^M"

会工作 - 如果 ^M 有你按照我的建议输入的文字 CR.

will work - if the ^M there is a literal CR that you input as I suggested.

如果需要文件列表,还需要添加 -l 选项.

If you want the list of files, you want to add the -l option as well.

说明

  • -I 忽略二进制文件
  • -U 防止 grep 剥离 CR 字符.默认情况下,如果它决定它是一个文本文件,它就会这样做.
  • -r 递归读取每个目录下的所有文件.
  • -I ignore binary files
  • -U prevents grep from stripping CR characters. By default it does this it if it decides it's a text file.
  • -r read all files under each directory recursively.

这篇关于如何在 Linux 上使用 grep 搜索包含 DOS 行尾 (CRLF) 的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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