Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out line-endings in a text file?

I'm trying to use something in bash to show me the line endings in a file printed rather than interpreted. The file is a dump from SSIS/SQL Server being read in by a Linux machine for processing.

  • Are there any switches within vi, less, more, etc?

  • In addition to seeing the line-endings, I need to know what type of line end it is (CRLF or LF). How do I find that out?

like image 477
Marco Ceppi Avatar asked Aug 25 '10 20:08

Marco Ceppi


People also ask

How can I tell if a file is CR LF or LF?

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. you can also go to View->Show Symbol->Show End Of Line to display the line ends as LF/ CR LF/CR.

How do you show line endings in Notepad ++?

First off, within Notepad ++ to see the end of line markers, you need to indicate you want to see them. Click on View > Show Symbol > then either Show End of Line, or Show All Characters if you want to see spaces and tabs, sometimes the second option is easier).

How can I tell how many lines are in a file?

The wc command is used to find the number of lines, characters, words, and bytes of a file. To find the number of lines using wc, we add the -l option. This will give us the total number of lines and the name of the file.

How do I view CR LF in vi?

vi shows newlines (LF character, code x0A ) by showing the subsequent text on the next line. Use the -b switch for binary mode. For example , vi -b filename or vim -b filename -- . It will then show CR characters ( x0D ), which are not normally used in Unix style files, as the characters ^M .


1 Answers

You can use the file utility to give you an indication of the type of line endings.

Unix:

$ file testfile1.txt testfile.txt: ASCII text 

"DOS":

$ file testfile2.txt testfile2.txt: ASCII text, with CRLF line terminators 

To convert from "DOS" to Unix:

$ dos2unix testfile2.txt 

To convert from Unix to "DOS":

$ unix2dos testfile1.txt 

Converting an already converted file has no effect so it's safe to run blindly (i.e. without testing the format first) although the usual disclaimers apply, as always.

like image 69
Dennis Williamson Avatar answered Oct 04 '22 14:10

Dennis Williamson