您的位置:首页 > 运维架构 > Linux

How to Convert Windows/DOS Text Files to Linux/UNIX Format

2014-07-17 11:03 676 查看
http://ubuntugenius.wordpress.com/2010/10/26/how-to-convert-windowsdos-text-files-to-linuxunix-format/

转过来有空细看。

line feed (\n or <LF>),

carriage return (\r or <CR>)

--------------------------------------------------------------------

October 26, 2010 by Ubuntu
Genius

A Quick Intro to End-Of-Line

Most people don’t realise that when they hit the Enter key to create a new paragraph in a text file, something very different is going on
behind the scenes in the three major operating systems: Windows, Macintosh and Linux. The “end-of-line
delimiter”
(often expressed as “End-Of-Line“, “End of Line“, or just “EOL“) – which some of you know as the “line break” or “newline” – is a special
character used to designate the end of a line within a text file
.

UNIX-based operating systems (like all Linux distros and BSD derivatives)
use the line feed character (\n or <LF>), “classic” Mac OS uses
a carriage return (\r or <CR>), while DOS/Windows uses
a carriage return followed by a line feed (\r\n or <CR><LF>). Now thatMac
OS X
is based on FreeBSD‘s file system, it follows the UNIX convention.

Now, the reason most people don’t know about all this is because nobody really should have to. But while users of Linux distros
and Mac OScan open Windows text files in basically any available editor and not even know the difference, the same can’t be said for Windows users
opening files created in one of the other operating systems.

If you type up a simple text file in Ubuntu and save it in the default “Unix/Linux
format, in Windows it will appear as one continuous paragraph, with black squares where the line breaks (or new paragraphs) should be. While you can open the file in a more advanced text editor
(or proper word processor) to view it as it should look, others you’ve sent it to are just likely to double-click it and let it open in Notepad (which can only handle MS-DOS EOL).

Occasionally, the reverse is the issue, but you can convert Windows text files to UNIX easily
with Gedit, as well as convert them via the terminal, so hopefully the following guide will be of use.

For more detailed info on End-Of-Line, go
to the Wikipedia page
.

Or if you’re wanting to do the reverse, check out how
to convert to Windows format via the terminal
and with Save
As…
in Gedit.

Converting Windows EOL to Linux via the Terminal

If you find the text editor you’re using to display Windows files in Ubuntu shows ^M instead
of a line break(not very likely with even the most lightweight text editors, but something you’ll probably come across if you display the text in a terminal), don’t worry – just
convert them toUnix/Linux format.

While you can actually open them in Gedit and use Save As… to save over them (or to create
copies) in the correct format, for more than a couple of files this would be the long, complicated solution.

By far the quickest and easiest approach is to convert the offending files via the command-line. This way, you could batch-convert hundreds of such files at once, not
have to do them individually.

There are actually quite a few ways to do this, but we’ll look at a couple of tiny packages you can install, and the related commands to use.

The first – the tofrodos package – is undoubtedly the most widely-used, so we’ll look at that in detail – especially
since many of the guides out there are outdated, since the commands it contains have been renamed.

The second is a little package called flip, and since it’s tiny and won’t cause any issues, it’s worth installing
as a backup (just in case. I found it useful after trying to get tofrodos going on a new system, before I found out the commands were changed).

There is no actual command tofrodos, as it is just the package that contains the commands todos and fromdos.
Currently, the vast majority of online guides will list the commands as unix2dos and dos2unix, but as the developer states:

With this release the symlinks “unix2dos” and
“dos2unix” are dropped from the package. This will allow the introduction of the original dos2unix package, which also supports conversion to MacOS style files.

So now you can choose to use either todos (to convert to Windows)
and fromdos (to convert to Linux), or just fromdos with options(fromdos -u to
convert to DOS, and fromdos -d to convert to UNIX,
though obviously the -d option really isn’t needed, as it is the default behaviour for the fromdos command).

We’ll use fromdos, as it is easier to remember, and show how to alter a single file, or all text files in a given
folder. When you’re ready to proceed, open
a terminal in the folder containing the text file(s)
and use one of the following commands (note that for the purpose of illustration, the .txt suffix is used, but you can specify any other extension for your text files).

To Convert to UNIX/Linux via Terminal:

Single file (remember to replace filename.txt with the actual name of the file)

fromdosfilename.txt

All text files in a folder (if the extension differs to .txt, simply replace it in
the command)

fromdos *.txt

Similarly, flip is easy to use:

flip -u filename.txt (or flip
-u *.
txt for multiple files)

Converting Windows EOL to Linux with Gedit

It’s actually very easy to convert text files with Windows EOL to Unix/Linux in Ubuntu using
the default Text Editor, Gedit. Simply open the files, choose Save As…, go to Line Ending in the dialogue
box and choose Unix/Linux instead of Windows. While that is easy enough, for more than one or two you’d really want to save yourself some time and hassle
and perform a batch-conversion via the terminal.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: