Crlf: Difference between revisions

From miki
Jump to navigation Jump to search
No edit summary
Line 3: Line 3:
== Reference ==
== Reference ==
* [http://vim.wikia.com/wiki/File_format File format in Vim]
* [http://vim.wikia.com/wiki/File_format File format in Vim]

== Miscellaneous ==
* Normal Linux line ending is LF
* Normal DOS line ending is CR-LF
* In Bash, insert special character with {{kbctrl|V}}. For instance {{kbctrl|V}} {{kbctrl|M}} will insert a CR character
** Alternatively, use something like <code>r="$(printf "\r")"</code>,
** Or use <code>echo $'\r'</code>


== Detection ==
== Detection ==
Line 14: Line 21:


* Use ''<tt>file</tt>'':
* Use ''<tt>file</tt>'':
<source lang="bash">
<source lang=bash>
file crlf.txt cr_and_lf.txt lf.txt
file crlf.txt cr_and_lf.txt lf.txt
# crlf.txt: ASCII text, with CRLF line terminators
# crlf.txt: ASCII text, with CRLF line terminators
# cr_and_lf.txt: ASCII text, with CRLF, LF line terminators
# cr_and_lf.txt: ASCII text, with CRLF, LF line terminators
lf# .txt: ASCII text
# lf.txt: ASCII text
find . -print0 | xargs -0 file | grep CR # Search all files
</source>

* Using GREP:
<source lang=bash>
grep -IUr --color $'\r' # Add -l to have list of files
find . -name "*.[ch]" -print0 | xargs -0 grep -IUl $'\r' # Prefilter files to look at
</source>
</source>

Revision as of 20:51, 18 July 2011

Everything about ascii file line termination issue (LF, CR/LF, CR...)

Reference

Miscellaneous

  • Normal Linux line ending is LF
  • Normal DOS line ending is CR-LF
  • In Bash, insert special character with ^V. For instance ^V ^M will insert a CR character
    • Alternatively, use something like r="$(printf "\r")",
    • Or use echo $'\r'

Detection

  • Hex dump
hd <file>
# 00000000  48 65 6c 6c 6f 0d 0a 48  65 6c 6c 6f 0d 0a 48 65  |Hello..Hello..He|
# 00000010  6c 6c 6f 0d 0a 48 65 6c  6c 6f 0d 0a              |llo..Hello..|
# 0000001c
  • Use file:
file crlf.txt cr_and_lf.txt lf.txt
# crlf.txt:       ASCII text, with CRLF line terminators
# cr_and_lf.txt:  ASCII text, with CRLF, LF line terminators
# lf.txt:         ASCII text
find . -print0 | xargs -0 file | grep CR                  # Search all files
  • Using GREP:
grep -IUr --color $'\r'                                   # Add -l to have list of files
find . -name "*.[ch]" -print0 | xargs -0 grep -IUl $'\r'  # Prefilter files to look at