Categories
DailyOps

How to convert text file in DOS format to Unix format

Convert line endings inside text file from DOS to UNIX format.

Try to execute shell script in DOS format.

$ ./processes_bar_chart.sh 
bash: ./processes_bar_chart.sh: /bin/sh^M: bad interpreter: No such file or directory

Install dos2unix utility.

$ sudo apt install dos2unix

Display usage information.

$ dos2unix  --help
Usage: dos2unix [options] [file ...] [-n infile outfile ...]
 --allow-chown         allow file ownership change
 -ascii                convert only line breaks (default)
 -iso                  conversion between DOS and ISO-8859-1 character set
   -1252               use Windows code page 1252 (Western European)
   -437                use DOS code page 437 (US) (default)
   -850                use DOS code page 850 (Western European)
   -860                use DOS code page 860 (Portuguese)
   -863                use DOS code page 863 (French Canadian)
   -865                use DOS code page 865 (Nordic)
 -7                    convert 8 bit characters to 7 bit space
 -b, --keep-bom        keep Byte Order Mark
 -c, --convmode        conversion mode
   convmode            ascii, 7bit, iso, mac, default to ascii
 -f, --force           force conversion of binary files
 -h, --help            display this help text
 -i, --info[=FLAGS]    display file information
   file ...            files to analyze
 -k, --keepdate        keep output file date
 -L, --license         display software license
 -l, --newline         add additional newline
 -m, --add-bom         add Byte Order Mark (default UTF-8)
 -n, --newfile         write to new file
   infile              original file in new-file mode
   outfile             output file in new-file mode
 --no-allow-chown      don't allow file ownership change (default)
 -o, --oldfile         write to old file (default)
   file ...            files to convert in old-file mode
 -q, --quiet           quiet mode, suppress all warnings
 -r, --remove-bom      remove Byte Order Mark (default)
 -s, --safe            skip binary files (default)
 -u,  --keep-utf16     keep UTF-16 encoding
 -ul, --assume-utf16le assume that the input format is UTF-16LE
 -ub, --assume-utf16be assume that the input format is UTF-16BE
 -v,  --verbose        verbose operation
 -F, --follow-symlink  follow symbolic links and convert the targets
 -R, --replace-symlink replace symbolic links with converted files
                         (original target files remain unchanged)
 -S, --skip-symlink    keep symbolic links and targets unchanged (default)
 -V, --version         display version number

Convert line endings from DOS to UNIX format.

$ dos2unix processes_bar_chart.sh 
dos2unix: converting file processes_bar_chart.sh to Unix format...

Execute shell script.

$ ./processes_bar_chart.sh 
                 username p-count |<                                          bar chart >|
                     _rpc       1 +
                    avahi       2 +
                   colord       1 +
                 kernoops       2 +
               messagebus       1 +
                   milosz     109 ++++++++++++++++++++++++++++++
      nvidia-persistenced       1 +
                     root     204 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                    rtkit       1 +
                    statd       1 +
                   syslog       1 +
              systemd-oom       1 +
          systemd-resolve       1 +
         systemd-timesync       1 +

I have used a shell script from awk-bar-chart repository to illustrate this issue.