Bash implementation for converting Unix files to dos/windows CRLF

bash
shell

#1

I currently use perl to convert Unix files to windows CRLF.
Unfortunately I need to keep the footprint small and I cannot use perl or any other add-ons such as sed/awk and perl.
It needs to be done with standard bash
Anyone have a standard bash implimentation for converting Unix files to dos/windows CRLF.
Somehow adding the Ascii characters for CRLF didnt work for me.

Thanks


#2

You can convert a file from DOS format to UNIX format (remove CRs from CR-LF line terminators) or vise versa. Perl/AWK/SED/ex/ed/tr are standard and installed on systems.

Use printf to convert a file from UNIX format ($) to DOS format (CR-LF)

The syntax is:

printf "This is a test\r\n" > test.txt
file test.txt
cat -v test.txt

Say you have a UNIX file named foo and want DOS format (CR-LF)

cat foo
This is a test
I love nixCraft
I like Unix

Run:

while IFS= read -r line; do printf "$line\r\n"; done < foo >> bar


printf is a shell builtin and so is bash while loop command.


#3

Perfect !
Thanks Nixcraft.
That solves it great.

For some reason I misunderstood and added the Ascii symbols for CR/LF at the end of lines which is why mine didnt work.


#4

Or, otherwise:

find ./ -type f -exec unix2dos {} \;

You’d need an apt install dos2unix though