How to determine the end of a file line

I have a bunch (hundreds) of files that should have Unix line endings. I strongly suspect that some of them have Windows line endings, and I want to programmatically figure out which ones to do.

I know that I can just run

  flip -u 
or something similar in a script to convert everything, but I want to be able to identify those files that need to be changed in the first place.
+46
scripting line-endings
Sep 23 '08 at 14:34
source share
7 answers

You can use grep

egrep -l $'\r'\$ * 
+28
Sep 23 '08 at 14:42
source share

You can use the file tool, which indicates the type of line ending. Or you can simply use dos2unix -U , which converts everything to the end of a Unix line, no matter where it starts.

+62
Sep 23 '08 at 14:40
source share

Something along the lines of:

 perl -p -e 's[\r\n][WIN\n]; s[(?<!WIN)\n][UNIX\n]; s[\r][MAC\n];' FILENAME 

although some of these regular expressions may need to be cleaned and cleaned.

This will output your file with WIN, MAC or UNIX at the end of each line. Well, if your file is somehow a terrible mess (or diff) and has mixed endings.

+14
Jan 14 '10 at 15:24
source share

Here is the most reliable answer. Stimms answers do not account for subdirectories and binaries

 find . -type f -exec file {} \; | grep "CRLF" | awk -F ':' '{ print $1 }' 
  • Use file to search for a file type. Those with CRLF have inverse Windows characters. The output of file is separated by the : character, and the first field is the path to the file.
+4
Jun 15 '16 at 21:41
source share

Unix uses one byte, 0x0A (LineFeed), and windows use two bytes, 0x0D 0x0A (carriage return, line feed).

If you never see 0x0D, then this is most likely Unix. If you see 0x0D 0x0A pairs, this is most likely MSDOS.

+3
Sep 23 '08 at 14:42
source share

Windows uses char 13 and 10 to end the line, unix is ​​only one of them (I don’t remember which one). That way you can replace char 13 and 10 with char 13 or 10 (one that uses unix).

0
Sep 23 '08 at 14:36
source share

When you know which files have Windows line endings ( 0x0D 0x0A or \r \n ), what will you do with these files? I suppose you convert them to the end of a Unix line ( 0x0A or \n ). You can convert a file with the end of a Windows line to the end of a Unix line using sed utility, just use the command:

 $> sed -i 's/\r//' my_file_with_win_line_endings.txt 

You can put it in a script like this:

 #!/bin/bash function travers() { for file in $(ls); do if [ -f "${file}" ]; then sed -i 's/\r//' "${file}" elif [ -d "${file}" ]; then cd "${file}" travers cd .. fi done } travers 

If you run it from the root directory with the files, at the end you will make sure that all the files end with a Unix line.

0
May 09 '15 at 9:00
source share



All Articles