Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Newline
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Conversion between newline formats === [[Text editor]]s are often used for converting a text file between different newline formats; most modern editors can read and write files using at least the different ASCII {{mono|CR}}/{{mono|LF}} conventions. For example, the editor [[Vim (text editor)|Vim]] can make a file compatible with the Windows Notepad text editor. Within vim <syntaxhighlight lang="vim"> :set fileformat=dos :wq </syntaxhighlight> Editors can be unsuitable for converting larger files or bulk conversion of many files. For larger files (on Windows NT) the following command is often used: <syntaxhighlight lang="doscon"> D:\>TYPE unix_file | FIND /V "" > dos_file </syntaxhighlight> Special purpose programs to convert files between different newline conventions include [[unix2dos|{{mono|unix2dos}} and {{mono|dos2unix}}]], {{mono|mac2unix}} and {{mono|unix2mac}}, {{mono|mac2dos}} and {{mono|dos2mac}}, and {{mono|flip}}.<ref>{{cite web |last1=Sapp |first1=Craig Stuart |title=ASCII text converstion between UNIX, Macintosh, MS-DOS |url=http://ccrma-www.stanford.edu/~craig/utility/flip/ |publisher=Center for Computer Research in Music and Acoustics |archive-url=https://web.archive.org/web/20090209015201/http://ccrma-www.stanford.edu/~craig/utility/flip/ |archive-date=9 February 2009}}</ref> The {{mono|[[tr (Unix)|tr]]}} command is available on virtually every [[Unix-like]] system and can be used to perform arbitrary replacement operations on single characters. A DOS/Windows text file can be converted to Unix format by simply removing all ASCII {{mono|CR}} characters with $ [[tr (Unix)|tr]] -d '\r' < ''inputfile'' > ''outputfile'' or, if the text has only {{mono|CR}} newlines, by converting all {{mono|CR}} newlines to {{mono|LF}} with $ [[tr (Unix)|tr]] '\r' '\n' < ''inputfile'' > ''outputfile'' The same tasks are sometimes performed with [[awk]], [[sed]], or in [[Perl]] if the platform has a Perl interpreter: <syntaxhighlight lang="console"> $ awk '{sub("$","\r\n"); printf("%s",$0);}' inputfile > outputfile # UNIX to DOS (adding CRs on Linux and BSD based OS that haven't GNU extensions) $ awk '{gsub("\r",""); print;}' inputfile > outputfile # DOS to UNIX (removing CRs on Linux and BSD based OS that haven't GNU extensions) $ sed -e 's/$/\r/' inputfile > outputfile # UNIX to DOS (adding CRs on Linux based OS that use GNU extensions) $ sed -e 's/\r$//' inputfile > outputfile # DOS to UNIX (removing CRs on Linux based OS that use GNU extensions) $ perl -pe 's/\r?\n|\r/\r\n/g' inputfile > outputfile # Convert to DOS $ perl -pe 's/\r?\n|\r/\n/g' inputfile > outputfile # Convert to UNIX $ perl -pe 's/\r?\n|\r/\r/g' inputfile > outputfile # Convert to old Mac </syntaxhighlight> The {{mono|[[File (command)|file]]}} command can identify the type of line endings: <syntaxhighlight lang="console"> $ file myfile.txt myfile.txt: ASCII English text, with CRLF line terminators </syntaxhighlight> The Unix [[grep#Implementations|egrep]] (extended grep) command can be used to print filenames of Unix or DOS files (assuming Unix and DOS-style files only, no classic Mac OS-style files): <syntaxhighlight lang="console"> $ egrep -L '\r\n' myfile.txt # show UNIX style file (LF terminated) $ egrep -l '\r\n' myfile.txt # show DOS style file (CRLF terminated) </syntaxhighlight> Other tools permit the user to visualise the EOL characters: <syntaxhighlight lang="console"> $ od -a myfile.txt $ cat -e myfile.txt $ cat -v myfile.txt $ hexdump -c myfile.txt </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)