Find and remove DOS line endings on Ubuntu

前端 未结 7 1497
攒了一身酷
攒了一身酷 2021-01-04 11:37

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

7条回答
  •  独厮守ぢ
    2021-01-04 12:07

    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.

    If it's a plain ASCII file, both of the below methods would work.

    The flip program, in Debian the package is also called flip, can handle line-endings. From the manual:

    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:

    < /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:

    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.

提交回复
热议问题