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

前端 未结 11 884
一向
一向 2020-11-28 00:15

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

相关标签:
11条回答
  • 2020-11-28 01:07

    To show CR as ^M in less use less -u or type -u once less is open.

    man less says:

    -u or --underline-special
    
          Causes backspaces and carriage returns to be treated  as  print-
          able  characters;  that  is,  they are sent to the terminal when
          they appear in the input.
    
    0 讨论(0)
  • 2020-11-28 01:09

    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.

    0 讨论(0)
  • 2020-11-28 01:11

    You can use vim -b filename to edit a file in binary mode, which will show ^M characters for carriage return and a new line is indicative of LF being present, indicating Windows CRLF line endings. By LF I mean \n and by CR I mean \r. Note that when you use the -b option the file will always be edited in UNIX mode by default as indicated by [unix] in the status line, meaning that if you add new lines they will end with LF, not CRLF. If you use normal vim without -b on a file with CRLF line endings, you should see [dos] shown in the status line and inserted lines will have CRLF as end of line. The vim documentation for fileformats setting explains the complexities.

    Also, I don't have enough points to comment on the Notepad++ answer, but if you use Notepad++ on Windows, use the View / Show Symbol / Show End of Line menu to display CR and LF. In this case LF is shown whereas for vim the LF is indicated by a new line.

    0 讨论(0)
  • 2020-11-28 01:15

    I dump my output to a text file. I then open it in notepad ++ then click the show all characters button. Not very elegant but it works.

    0 讨论(0)
  • 2020-11-28 01:19

    Ubuntu 14.04:

    simple cat -e <filename> works just fine.

    This displays Unix line endings (\n or LF) as $ and Windows line endings (\r\n or CRLF) as ^M$.

    0 讨论(0)
提交回复
热议问题