1

I have a csv file that I'm intending to import into a mysql table. I would therefore like to make sure I'm reporting it's LINES TERMINATED BY properly. However I cannot figure out what characters are terminating the lines in this csv.

I'd guess that it's terminated by \n (the standard Unix EOL). How can I determine this for sure?

I've tried

  • cat -v file.csv
  • file file.csv

I've also tried using vim and :set list which just showed the location of line breaks with $

Any suggestions would be appreciated

3 Answers 3

3

You can create a hex-dump of your file with xxd which is part of the vim-common package.

xxd file.csv | less

Then check the line endings:

  • 0a => \n
  • 0d => \r
  • 0d0a => \r\n
2
  • od -t x1z | less - should be similar, without requiring an extra package.
    – Hannu
    Commented Jun 1, 2015 at 20:44
  • Thats very good, for a large file you can even reverse it: head file.csv | xxd |greo --color 0a will show it. Commented Oct 29, 2022 at 2:09
1

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

Unix:

$ file file1.txt
file1.txt: ASCII text

DOS:

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

and knowing that DOS uses carriage return and line feed ("\r\n") as a line ending, which Unix uses just line feed ("\n").

So you can determine what is EOL of any file you want.

3
  • Thanks for the response. I've seen more exotic line terminations in different examples for example: stackoverflow.com/q/21300075/2540204. Now I'm certain that's not the case here but how would I know if it was? I feel like I should be able to examine it directly. Commented May 29, 2015 at 8:12
  • ...Incidentally the result form file is file.csv: ASCII text Commented May 29, 2015 at 8:13
  • Then it's Unix so the \n would be EOL
    – Maythux
    Commented May 29, 2015 at 8:14
1

Instead of CHECKING, why not simply SET the line ending style you prefer:

sudo apt-get dos2unix will give you access to dos2unix and unix2dos command line utilities.

These will convert text files according to the names.

Caveat: files with mixed content has been troublesome, I'm not updated on whether that problem is gone.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .