linux poison RSS
linux poison Email

Bash Script: Convert File from DOS to UNIX format

Common problem! If you need to exchange text files between DOS/Windows and Linux, be aware of the "end of line" problem. Under DOS, each line of text ends with CR/LF (Carriage return/Line feed), with LF under Linux. If you edit a DOS text file under Linux, each line will likely end with a strange--looking `M' character;

Below is simple shell script which converts the files in DOS format to Unix format
Feel free to copy and use this code

Source: cat dos_unix.sh
#!/bin/bash

if [ $# -ne 1 ]; then
        echo "Please provide the path of a valid DOS file"
        exit
fi

if [ ! -f "$1" ]; then
        echo "Cannot access file: $1"
        echo "Don't play, provide the vaild file."
        exit
fi

DOSFILE=$1
UNIXFILE=output.unix
CR='\015'

tr -d $CR < $DOSFILE > $UNIXFILE

echo "Done with the conversion"
echo "New output file is: $UNIXFILE"
exit

Output: ./dos_unix.sh server.txt
Done with the conversion
New output file is: output.unix




3 comments:

Anonymous said...

Why not use dos2unix and unix2dos?

DevOps said...

Yes, we can use these utilities, above script just shows how these utilities works.

Fuel Injection Pipes said...

Excellent!
This is a great help for file conversion..

Post a Comment

Related Posts with Thumbnails