convert dos2unix line endings for all files in a directory
for i in `find . -type f \( -name "*.c" -o -name "*.h" \)`; do sed -i 's/\r//' $i ; done
What's the best way of doing dos2unix on a 500k line file, in Windows?
Here is a Perl one-liner, taken from http://www.technocage.com/~caskey/dos2unix/
#!/usr/bin/perl -pi
s/\r\n/\n/;
You can run it as follows:
perl dos2unix.pl < file.dos > file.unix
Or, you can run it also in this way (the conversion is done in-place):
perl -pi dos2unix.pl file.dos
And here is my (naive) C version:
#include <stdio.h>
int main(void)
{
int c;
while( (c = fgetc(stdin)) != EOF )
if(c != '\r')
fputc(c, stdout);
return 0;
}
You should run it with input and output redirection:
dos2unix.exe < file.dos > file.unix
Unix newlines to Windows newlines (on Windows)
Here is the pure PowerShell way if you are interested.
Finding files with at least one Unix line ending (PowerShell v1):
dir * -inc *.txt | %{ if (gc $_.FullName -delim "`0" | Select-String "[^`r]`n") {$_} }
Here is how you find and covert Unix line endings to Windows line endings. One important thing to note is that an extra line ending (\r\n) will be added to the end of the file if there isn't already a line ending at the end. If you really don't want that, I'll post an example of how you can avoid it (it is a bit more complex).
Get-ChildItem * -Include *.txt | ForEach-Object {
## If contains UNIX line endings, replace with Windows line endings
if (Get-Content $_.FullName -Delimiter "`0" | Select-String "[^`r]`n")
{
$content = Get-Content $_.FullName
$content | Set-Content $_.FullName
}
}
The above works because PowerShell will automatically split the contents on \n (dropping \r if they exist) and then add \r\n when it writes each thing (in this case a line) to the file. That is why you always end up with a line ending at the end of the file.
Also, I wrote the above code so that it only modifies files that it needs to. If you don't care about that you can remove the if
statement. Oh, make sure that only files get to the ForEach-Object. Other than that, you can do whatever filtering you want at the start of that pipeline.
Converting newline formatting from Mac to Windows
Windows uses carriage return
+ line feed
for newline:
\r\n
Unix only uses Line feed
for newline:
\n
In conclusion, simply replace every occurence of \n
by \r\n
.
Both unix2dos
and dos2unix
are not by default available on Mac OSX.
Fortunately, you can simply use Perl
or sed
to do the job:
sed -e 's/$/\r/' inputfile > outputfile # UNIX to DOS (adding CRs)
sed -e 's/\r$//' inputfile > outputfile # DOS to UNIX (removing CRs)
perl -pe 's/\r\n|\n|\r/\r\n/g' inputfile > outputfile # Convert to DOS
perl -pe 's/\r\n|\n|\r/\n/g' inputfile > outputfile # Convert to UNIX
perl -pe 's/\r\n|\n|\r/\r/g' inputfile > outputfile # Convert to old Mac
Code snippet from:
http://en.wikipedia.org/wiki/Newline#Conversion_utilities
Related Topics
How to Read Single Character Input from Keyboard Using Nasm (Assembly) Under Ubuntu
Command Line Utility to Print Statistics of Numbers in Linux
Linux Time Command Microseconds or Better Accuracy
How to Change the Filename of a Shared Library After Building a Program That Depends on It
Does Gcc Have Any Options to Add Version Info in Elf Binary File
Sort Not Sorting as Expected (Space and Locale)
Repeat Command Automatically in Linux
Prevent Gnome Terminal from Exiting After Execution
Using Grep and Sed to Find and Replace a String
How to Check If Sed Has Changed a File
What's the Point of Eval/Bash -C as Opposed to Just Evaluating a Variable
Create Zip File and Ignore Directory Structure
How to Check If X Server Is Running
How to Undo Strip - I.E. Add Symbols Back to Stripped Binary