Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On windows, how would I detect the line ending of a file?

I've seen answers to the questions, but those answers are not from a windows perspective from what I can tell.

Windows uses CR LF, Unix uses LF, Mac uses LF and classic mac uses something else. I don't have the brainpower to tell that somehow, if a file is using a different line ending than what I am typing, I get errors when trying to run the script/program which frankly, don't make much sense. After conversion, the script works just fine.

Is there anyway to preemptively check what line endings a file uses, on Windows?

like image 211
13steinj Avatar asked Aug 27 '15 17:08

13steinj


People also ask

How do I find the end of a file with line?

Try file -k txt will tell you. It will output with CRLF line endings for DOS/Windows line endings. It will output with CR line endings for MAC line endings. And for Linux/Unix line "LF" it will just output text .

What is line ending in Windows?

Text files created on DOS/Windows machines have different line endings than files created on Unix/Linux. DOS uses carriage return and line feed ("\r\n") as a line ending, which Unix uses just line feed ("\n").

How do I know if a file has carriage return?

If a file has DOS/Windows-style CR-LF line endings, then if you look at it using a Unix-based tool you'll see CR ('\r') characters at the end of each line.

What is end of line sequence?

The End of Line (EOL) sequence ( 0x0D 0x0A , \r\n ) is actually two ASCII characters, a combination of the CR and LF characters. It moves the cursor both down to the next line and to the beginning of that line.


3 Answers

use a text editor like notepad++ that can help you with understanding the line ends.

It will show you the line end formats used as either Unix(LF) or Macintosh(CR) or Windows(CR LF) on the task bar of the tool.

enter image description here

you can also go to View->Show Symbol->Show End Of Line to display the line ends as LF/ CR LF/CR.

enter image description here

like image 180
Anil D Avatar answered Oct 28 '22 04:10

Anil D


Steps:

  • From the following link download binaries and dependencies zip files: http://gnuwin32.sourceforge.net/packages/file.htm
  • Extract their content under the same directory (merge existing directories).
    e.g. under c:\gnuwin32

Then you can execute:

c:\gnuwin32\bin\file.exe my-lf-file.txt

my-lf-file.txt; ASCII text

c:\gnuwin32\bin\file.exe my-crlf-file.txt

my-crlf-file.txt; ASCII text, with CRLF line terminators

Of course you can add c:\gnuwin32\bin to your %PATH% variable, to be able to access it without providing the full path.


UPDATE:

  • If you have git installed you can launch git-bash and run file command from there.

  • Or you can install this subsystem, as described in the official Microsoft documentation, and get access to the file command.

like image 32
Marinos An Avatar answered Oct 28 '22 04:10

Marinos An


I too am looking for a "native" windows scripting solution. So far, just have to read a line or 2 in VB in binary fashion and inspect the characters.

One tool to check "manually" is Notepad++. The status bar has a newline style indicator on the right end next to the file encoding indicator.

It looks like this in version 7.5.6 enter image description here

Other editors with Hex mode can show you also.

In Powershell, this command returns "True" for a Windows style file and "False" for a *nix style file.

(Get-Content '\\FILESERVER0001\Fshares\NETwork Shares\20181206179900.TXT' -Raw) -match "\r\n$" 

This came from Matt over here: https://stackoverflow.com/a/35354009/1337544

like image 43
charles ross Avatar answered Oct 28 '22 05:10

charles ross