37

I recently moved back to Windows from Linux. I have some files with CRLFs, some with LFs and some that are mixed. Is there a utility that will help me find all my Unix-touched files and convert them to proper CRLF terminated files?

The utility must run on Windows, not Linux. I have already moved. I'd rather not install Cygwin if I can avoid it.

  • 1
    related: see superuser.com/questions/38744 superuser.com/questions/27060 superuser.com/questions/52044 ... any tool mentioned in those can be reversed to accomplish what you want to do – quack quixote Nov 17 '09 at 4:59
  • Duplicate question – ukanth Nov 17 '09 at 9:40
  • The solutions in those so called Duplicate questions all run on linux platforms or preform the opposite conversion. I don't have linux any more. I have windows. – Matthew Scouten Nov 17 '09 at 14:50
  • 1
    they are all available for Windows via Cygwin, GnuWin32, UnxUtils or the like. i believe there are Powershell scripts that can do this as well, though i don't know enough about it to provide any links. – quack quixote Nov 17 '09 at 15:00
  • 1
    @quack: The question specifically states that the user is looking for something that will do the job on Windows, using a Windows utility. – Brian Lacy Nov 19 '10 at 21:00

13 Answers 13

33

You can convert them with the unix2dos utility on your Linux platform. There are unix2dos versions available for Windows as well.

If you have Perl installed you can also use this one liner:

perl -p -e 's/\n/\r\n/' < UNIX_LF.txt > WINDOWS_CRLF.txt
  • I did something similar to this, but I used python. – Matthew Scouten Nov 19 '09 at 2:27
  • 2
    @Matthew Scouten: so... you found a unix utility that runs on windows! congrats! – quack quixote Nov 19 '09 at 13:31
  • 8
    yes and no. I WROTE my own blasted utility. I could have done this from the start, but I was hoping to save 30 mins. Unfortunately I was not happy with any utilities available. The closest I found corrupted any binary files in the directory. – Matthew Scouten Nov 20 '09 at 15:08
  • There are problems converting with dos2unix and unix2dos - if the file has mixed CRLF and LF there may be some occurences that wouldn't be replaced. – pbies Jun 16 '18 at 13:03
17

Here is an easy and quick way.

Drag and drop the text file into Chrome (I don't know about other browsers) and then cut and paste back into the original file :)

  • 2
    hehe… this answer made me lol – ClintM Feb 20 '17 at 17:37
  • all browsers can read Unix line endings just fine, including IE. But even then wordpad can also do that, and so will modern Notepad. What's important here is how to convert multiple files automatically – phuclv May 29 '18 at 14:13
10

The one I found best for recursively going through folders, allowing file filters and allowing a simple search for "\r\n" and replacing it with just "\n" was Notepad++.

Notepad++ is one of the best, free, open source notepad programs for Windows. It is very simple and powerful. It handled the line ending search/replace just fine. A contractor check a bunch of .c and .h files in to our repository with Linux \r\n line endings, but since most people have standardized on Windows/Eclipse build tools, the files won't build until the line endings are converted.

  • 8
    I think you reversed the endings there: linux is \n, windows is \r\n – Matthew Scouten Apr 8 '11 at 7:12
  • 10
    Notepad++ also has a simple Edit -> EOL Conversion menu if the number of files to convert is small. – user1071136 Jul 24 '12 at 22:21
8

Use the Swiss File Knife.

For example: sfk addcr -dir . -file .txt -norec
changes LF endings into CR/LF for Windows, on all .txt files of the current directory, but NOT within subdirectories (no recursion).

But this program does a lot more than just that.

  • I don't specify -norec because I want recursion to happen but it doesn't seem like it – Csaba Toth Dec 23 '16 at 5:23
6

On Cygwin, you can convert between Unix and "DOS" AKA Windows files using two built-in utilities:

Convert to DOS CR/LF format:

u2d filename

Convert back to Unix CR format:

d2u filename

The file is left in place with the same name.

Enjoy! Rick

  • This is sometimes called dos2unix or unix2dos in case you cannot find it as u2d or d2u. There is also unix2mac – gtatr Jun 19 '18 at 13:28
3

I'm going to throw this solution out there. Git will do this. See this post about it

So theoretically you could do this to convert an entire tree

cd root/of/tree
git init .
git add .
git commit -m "initial commit"
echo "* text eol=crlf" > .gitattributes
git rm --cached -r .
git reset --hard

Change crlf to lf if you want to go the other way. NOTE: you're not done yet, keep reading

Type git status to see which files will be affected. You might have to add lines like

*.jpg binary
*.png binary
*.gif binary

etc to .gitattributes to avoid converting certain files. You can also explicit mark certain files as text

*.md text
*.css text

Then just repeat these 2 lines after you've edited .gitattributes

git rm --cached -r .
git reset --hard

Then use git status again to see which files will be changed. When you're sure all the files you want affected are listed by git status then commit

git add .
git commit -m "normalize line endings"

now check all the files out again

git rm --cached -r .
git reset --hard

They should now have whatever your desired line endings are

** NOTE: If you were already using git skip the first 3 commands git commands. If you were not using git you can now delete the .gitattributes file and the .git folder.

** Back up your files: the git rm --cached -r deletes them all (although they are theoretically in your git repo (the .git folder) which is how they get restored by the last command git reset --hard. It's just since files are getting deleted it's probably best to back them up.

1

Use a text editor that understands both line endings like SciTE, or Notepad++ if you don't need to convert all line ending in all your files, but just don't want to see the entire file bunched up on the first line.

  • That's what they said when I had the inverse problem while switching to linux. I kept running into tools that had problems with the wrong line endings. And NOTHING handles mixed line endings reasonably. – Matthew Scouten Mar 31 '11 at 17:05
  • @Matthew I know the scite handle unix / windows line endings correctly. I use it for that purpose in the windows vms I run in linux when editing files on the host system. – nelaaro Apr 1 '11 at 10:54
  • I am sure that the editors you recommend will work just fine. but at some point, I will need to feed the textfile to some other tool, and that tool will NOT. At that point, having a text editor that handles things well will make it worse by hiding the problem. and I will bet that it still handles mixed endings badly (or at least with quirks). – Matthew Scouten Apr 1 '11 at 13:44
  • 1
    +1 for the notepad++ option to convert lie endings. And here is how to do more specifically (see "method 2") : staffwww.fullcoll.edu/brippe/csci123/saveAsUnix.aspx – wip May 30 '12 at 1:34
  • Notepad++ already have the solutions to convert line endings, encodings among other things – phuclv Mar 12 '17 at 5:41
0

There are many ways to translate the eoln characters in TEXT files, and everyone has a favourite.

But I always transfer files from Linux to Windows in BINARY mode, then I open TEXT files in Windows with an editor capable of opening both types, and saving them in either form, if necessary.

I used Programmers File Editor for this, but Notepad++ can do it too. WordPad is also useful [at least, for viewing LF terminated files].

I'm thinking of the simple text files which may have originated on Linux, and need to be readable in the (defacto standard) world of Windows. I'm not sure what you meant by 'unix-touched files'.

  • The problem with this method is that it only does one file at a time. – Matthew Scouten Nov 19 '09 at 2:23
  • 'unix-touched files' is my word for both LF-only and mixed CRLF-LF files. Once a file has been edited in unix-oid tools, it tends to get 'infected' with LF-only lines. – Matthew Scouten Nov 19 '09 at 2:26
  • 2
    What I was really saying was that by choosing the right software for viewing text files, they don't have to be converted at all. BTW, I avoided taking you to task for describing CRLF terminated files as 'proper'. It depends entirely on your viewpoint. Each method is proper in its own OS. When a file is moved to a different OS, then we find that Win/Mac/Linux are all different. I suspect that the Unix/Linux method is the original one. – pavium Nov 19 '09 at 2:38
  • 3
    You suspect wrong. ASCII standard was originally developed for teletype machines, and mandated CRLF. Most internet protocols (including HTTP, SMTP, FTP, IRC and many others) also require CRLF endings (most implementations accept bare LF for compatibility with non-compliant Unix programs). In this case, Windows is following the standard and Unix and Mac are breaking it. – Matthew Scouten Jul 16 '10 at 4:06
0

To offer more options (even though I best enjoyed user45832's hack answer):

Online convertors

I guess a GUI FTP program will do it automatically

Batch/DOS one-liner (removes empty lines):

FOR /F "eol= delims= usebackq" %a IN (infile.txt) DO (ECHO %a>> outfile.txt)

Powershell:

gc infile.txt | %{$_.split("`n")} | Out-File outfile.txt

Got the last two from here

0

Get AWK for Windows.

Convert Unix line endings to Windows line endings:

awk 'sub("$", "\r")' unixfile.txt > winfile.txt

Convert Windows line endings to Unix line endings:

awk '{ sub("\r$", ""); print }' winfile.txt > unixfile.txt

Older versions of awk do not include the sub function. In such cases, use the same command, but replace awk with gawk or nawk.

Source

0

On Wikipedia there's a solution in cmd:

TYPE unix_file | FIND /V "" > dos_file

In PowerShell there are various ways to do that by reversing what was done in this question.

(Get-Content $file -Raw).Replace("`n", "`r`n") | Set-Content $path -Force

(Get-Content $infile) -join "`r`n" > $outfile

$text = [IO.File]::ReadAllText($original_file) -replace "`n", "`r`n"
[IO.File]::WriteAllText($original_file, $text)

It's also possible to do this in VBScript and JScript which are also tools that are already available in Windows without installing a third-party application.

0

My Linux distribution has two little utilities called fromdos and todos that I use for this task.

  • The OP is asking for a Windows tool – phuclv May 29 '18 at 14:08
-2

I used to open the file in 'edit' and save as that the job was done ...

  • what's "edit"? there's no such tool in windows 10 – phuclv May 29 '18 at 14:02
  • computerhope.com/edithlp.htm – Radek Jun 16 '18 at 12:45
  • obviously I've used edit in DOS but this question is about Windows – phuclv Jun 16 '18 at 12:51
  • 1
    edit used to be part of Windows up to Windows XP. My answer is about 8 years old. In that time edit was part of Windows installation in many cases ... – Radek Jun 18 '18 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.