{"id":121,"date":"2010-03-07T18:39:57","date_gmt":"2010-03-07T16:39:57","guid":{"rendered":"http:\/\/www.ceh-photo.de\/blog\/?p=121"},"modified":"2010-05-05T16:21:13","modified_gmt":"2010-05-05T14:21:13","slug":"bash-script-for-converting-dos-to-unix-textfiles","status":"publish","type":"post","link":"https:\/\/www.ceh-photo.de\/blog\/?p=121","title":{"rendered":"Bash Script for converting DOS to UNIX textfiles"},"content":{"rendered":"<p>This is a short bash script, which I have written as a homework in my studies. It converts all files in parameters from DOS to UNIX or from UNIX to DOS, depending on their current style.<\/p>\n<div class=\"codecolorer-container bash default\" style=\"overflow:auto;white-space:nowrap;width:600px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/>43<br \/><\/div><\/td><td><div class=\"bash codecolorer\"><span class=\"co0\">#!\/bin\/bash<\/span><br \/>\n<br \/>\n<span class=\"co0\">#fromdos - todos<\/span><br \/>\n<span class=\"co0\"># This scripts check file(s) in parameter(s) for line ends in unix or dos style and converts to the opposit one<\/span><br \/>\n<span class=\"co0\"># if file(s) does not exist a error is thrown<\/span><br \/>\n<br \/>\n<span class=\"co0\">#Author:Christopher-Eyk Hrabia<\/span><br \/>\n<span class=\"co0\">#Date:&nbsp; 31.10.2009<\/span><br \/>\n<br \/>\n<span class=\"re2\">TEMPFILE<\/span>=<span class=\"st0\">&quot;\/tmp\/fromDOS_toDOS.txt&quot;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#91;<\/span><span class=\"br0\">&#91;<\/span> <span class=\"re4\">$#<\/span> <span class=\"re5\">-eq<\/span> <span class=\"nu0\">0<\/span> <span class=\"br0\">&#93;<\/span><span class=\"br0\">&#93;<\/span>;<span class=\"kw1\">then<\/span><br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Using:&quot;<\/span><br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;fromDosToDos [file] ...&quot;<\/span><br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Explanation:&quot;<\/span><br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Every file in arguments will be converted to his opposit,&quot;<\/span><br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;it means: it converts DOS style to UNIX style and otherwise&quot;<\/span><br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Author: Christopher-Eyk Hrabia - c.hrabia@gmail.com&quot;<\/span><br \/>\n<span class=\"kw1\">fi<\/span><br \/>\n<br \/>\n<span class=\"re2\">args<\/span>=$<span class=\"sy0\">@<\/span><br \/>\n<span class=\"kw1\">for<\/span> i <span class=\"kw1\">in<\/span> <span class=\"re1\">$args<\/span>;<span class=\"kw1\">do<\/span><br \/>\n<span class=\"kw1\">if<\/span> <span class=\"kw3\">test<\/span> <span class=\"re5\">-a<\/span> <span class=\"re1\">$i<\/span>;<span class=\"kw1\">then<\/span><br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;File converted: <span class=\"es2\">$i<\/span>&quot;<\/span><br \/>\n<br \/>\n<span class=\"co0\">#check if DOS or UNIX<\/span><br \/>\n<span class=\"re2\">countMS<\/span>=<span class=\"sy0\">`<\/span><span class=\"kw2\">cat<\/span> <span class=\"re1\">$i<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">sed<\/span> <span class=\"re5\">-n<\/span> <span class=\"st_h\">'\/\\x0D$\/p'<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">wc<\/span> -l<span class=\"sy0\">`<\/span><br \/>\n<br \/>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#91;<\/span><span class=\"br0\">&#91;<\/span> <span class=\"re1\">$countMS<\/span> <span class=\"re5\">-gt<\/span> <span class=\"nu0\">0<\/span> <span class=\"br0\">&#93;<\/span><span class=\"br0\">&#93;<\/span>;<span class=\"kw1\">then<\/span><br \/>\n<span class=\"co0\">#DOS to Unix<\/span><br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;to UNIX&quot;<\/span><br \/>\n<span class=\"kw2\">sed<\/span> <span class=\"st_h\">'s\/\\x0D$\/\/'<\/span> <span class=\"re1\">$i<\/span> <span class=\"sy0\">&amp;<\/span>gt; <span class=\"re1\">$TEMPFILE<\/span><br \/>\n<span class=\"kw1\">else<\/span><br \/>\n<span class=\"co0\">#Unix to DOS<\/span><br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;to DOS&quot;<\/span><br \/>\n<span class=\"kw2\">sed<\/span> <span class=\"st_h\">'s\/$\/\\x0D\/'<\/span> <span class=\"re1\">$i<\/span> <span class=\"sy0\">&amp;<\/span>gt; <span class=\"re1\">$TEMPFILE<\/span><br \/>\n<span class=\"kw1\">fi<\/span><br \/>\n<span class=\"kw2\">mv<\/span> <span class=\"re1\">$TEMPFILE<\/span> <span class=\"re1\">$i<\/span><br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st_h\">'+++++++++++++++++++++++++++++++++++++++++++'<\/span><br \/>\n<span class=\"kw1\">else<\/span><br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;File does not exist: <span class=\"es2\">$i<\/span>&quot;<\/span><span class=\"sy0\">&amp;<\/span>gt;<span class=\"sy0\">&amp;<\/span>amp;<span class=\"nu0\">2<\/span><br \/>\n<span class=\"kw1\">fi<\/span><br \/>\n<span class=\"kw1\">done<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>have fun with it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a short bash script, which I have written as a homework in my studies. It converts all files in parameters from DOS to UNIX or from UNIX to DOS, depending on their current style. 12345678910111213141516171819202122232425262728293031323334353637383940414243#!\/bin\/bash #fromdos &#8211; todos # This scripts check file(s) in parameter(s) for line ends in unix or dos style [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,46],"tags":[135,39,40,41],"class_list":["post-121","post","type-post","status-publish","format-standard","hentry","category-it","category-linux-it-2","tag-ba","tag-bash","tag-linux","tag-shell"],"_links":{"self":[{"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=121"}],"version-history":[{"count":10,"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/121\/revisions"}],"predecessor-version":[{"id":139,"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/121\/revisions\/139"}],"wp:attachment":[{"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}