How do you search for files containing DOS line endings (CRLF) with grep on Linux?

后端 未结 9 1197
栀梦
栀梦 2020-11-30 17:24

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

grep -IUr --color \'\\         


        
相关标签:
9条回答
  • 2020-11-30 18:03

    The query was search... I have a similar issue... somebody submitted mixed line endings into the version control, so now we have a bunch of files with 0x0d 0x0d 0x0a line endings. Note that

    grep -P '\x0d\x0a'
    

    finds all lines, whereas

    grep -P '\x0d\x0d\x0a'
    

    and

    grep -P '\x0d\x0d'
    

    finds no lines so there may be something "else" going on inside grep when it comes to line ending patterns... unfortunately for me!

    0 讨论(0)
  • 2020-11-30 18:11

    If your version of grep supports -P (--perl-regexp) option, then

    grep -lUP '\r$'
    

    could be used.

    0 讨论(0)
  • 2020-11-30 18:18

    dos2unix has a file information option which can be used to show the files that would be converted:

    dos2unix -ic /path/to/file
    

    To do that recursively you can use bash’s globstar option, which for the current shell is enabled with shopt -s globstar:

    dos2unix -ic **      # all files recursively
    dos2unix -ic **/file # files called “file” recursively
    

    Alternatively you can use find for that:

    find -exec dos2unix -ic {} +            # all files recursively
    find -name file -exec dos2unix -ic {} + # files called “file” recursively
    
    0 讨论(0)
提交回复
热议问题