0

I'm writing a simple shell script to make use of dos2unix command to convert Windows-format files to Unix format as and when it arrives in my folder.

I used to use iconv in the script and automate it to get one encoding converted to the other. But now I need to use dos2unix instead of iconv.

I don't want the original file to be overwritten (it must be archived in the archive folder). This was straightforward with iconv; how can I do the same with dos2unix?

This is my script:

cd /myfolder/storage
filearrival_dir= /myfolder/storage
filearchive_dir=/myfolder/storage/archive

cd $filearrival_dir
echo " $filearrival_dir"
for file in File_October*.txt
do
    iconv -f UTF16 -t UTF8  -o "$file.new" "$file"   &&
    mv -f "$file.new" "$file".`date +"%C%y%m%d"`.txt_conv &&
    mv  $file $filearchive_dir/$file
 done

The above looks for files matching File_Oct*.txt, converts to the desired encoding and renames it with the timestamp and _conv at the end. This script also moves the original file to the archive.

How can I replace iconv in the above script with dos2unix and have the files archived and do the rest just like I did here?

Toby Speight
  • 23,550
  • 47
  • 57
  • 84
mac_online
  • 165
  • 2
  • 13

2 Answers2

2

You can "emulate" dos2unix using tr.

tr -d '\015' infile > outfile

Arun Kumar
  • 57
  • 1
  • 11
0

If this is just about using dos2unix so it doesn't over-write the original file, just use

-n infile outfile

My recollection is that dos2unix writes UTF-8 by default, so you probably don't have to take any special action so far as encoding is concerned.

Kevin Boone
  • 3,705
  • 1
  • 7
  • 14