skip to Main Content

I need to find what type of line break that is used in a csv file, using Mac. I have exported a data set from SPSS (a statistical software) to a CSV-file. This CSV-file will be sent to be run through a register and I need to provide information regarding the file, such as which line break-style that is used in the file.

As I open the CSV-file in TextEdit on my Mac I see no symbols corresponding to line break (does not say rn, r or n. There is simply a new row with no symbol indicating line break. I have not been able to find what’s used as default in SPSS or how to customize this. I tried by running the file through the Terminal-app and Visual studio code (what I had access to) but no symbols indicating line break. Does anyone know how to determine which line break-style that is used in the CSV-file in this case?

2

Answers


  1. You can open the file in Visual Studio using the Binary editor. You will see all characters.

    Login or Signup to reply.
  2. According the RFC 4180, CRLF is the standard record delimiter for CSVs, but LF is also frequently used. Forget about CR-delimited records as that kind of CSV probably doesn’t exist anymore.

    Here’s a solution that works in most cases:

    awk '{print (/r$/ ? "CRLF" : "LF"); exit}' file.csv
    

    The problem with the previous approach is that a CSV record can span multiple lines, so encountering a LF doesn’t guaranty that you got to the end of the record. A workaround would be to go to the end of the file and check how it is terminated.

    You can use perl for that:

    perl -le '
        open(F, '<', $ARGV[0]) or die $!."n";
        seek(F, -2, 2);
        read(F, $e, 2);
        close(F);
        if("rn" eq $e) {print "CRLF"}
        elsif("n" eq ($e = substr($e, -1))) {print "LF"}
        elsif($e eq "r") {print "CR"}
    ' file.csv
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search