{"id":452,"date":"2011-02-24T09:44:31","date_gmt":"2011-02-24T16:44:31","guid":{"rendered":"http:\/\/www.coastalvectors.com\/blog\/?p=452"},"modified":"2015-01-16T15:35:57","modified_gmt":"2015-01-16T22:35:57","slug":"utf-8-converter","status":"publish","type":"post","link":"https:\/\/www.coastalvectors.com\/blog\/2011\/02\/utf-8-converter\/","title":{"rendered":"UTF-8 Converter"},"content":{"rendered":"<p style=\"text-align: left;\"><a href=\"http:\/\/www.coastalvectors.com\/blog\/wp-content\/uploads\/2011\/02\/temrinal.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-463\" title=\"temrinal\" src=\"http:\/\/www.coastalvectors.com\/blog\/wp-content\/uploads\/2011\/02\/temrinal-300x300.jpg\" alt=\"\" width=\"210\" height=\"210\" srcset=\"https:\/\/www.coastalvectors.com\/blog\/wp-content\/uploads\/2011\/02\/temrinal-300x300.jpg 300w, https:\/\/www.coastalvectors.com\/blog\/wp-content\/uploads\/2011\/02\/temrinal-150x150.jpg 150w, https:\/\/www.coastalvectors.com\/blog\/wp-content\/uploads\/2011\/02\/temrinal.jpg 400w\" sizes=\"auto, (max-width: 210px) 100vw, 210px\" \/><\/a><br \/>\nThe other day at work, I needed to batch convert about one or two hundred files formatted in MACROMAN format to UTF-8. Well, it turns out there is a command line utility to do just this called iconv. I was very pleased when I found that because it was going to save me a lot of time. Then I ran it and got confused. It turns out that iconv does convert text format, but it doesn&#8217;t write it back out to a file, it just spits the results back into the terminal window. Mildly frustrated, I decided to take matters into my own hands and write a script that would take the output and put it back into a file with the same name. These are the results:<\/p>\n<p><code>#!\/bin\/bash<br \/>\n<br \/>\nfor f in $1\/* ; do<br \/>\no=`basename $f`<br \/>\nif file $f | grep Unicode ; then<br \/>\ncp $f $2<br \/>\nelse<br \/>\niconv -f MACROMAN -t UTF-8 $f &gt;$2\/$o<br \/>\nfi<br \/>\ndone<\/code><\/p>\n<p>I went further and added options,\u00a0 a debug mode, verbose mode, and the like, and even a man page! The syntax is:<\/p>\n<p><code># roman_to_utf8 [options] &lt;input&gt; &lt;output&gt;<\/code><\/p>\n<p>The input and output can be either directories or individual files.<\/p>\n<p><code>#!\/bin\/bash<br \/>\n<br \/>\nusage() {<br \/>\necho Usage: $0 \"[-v | --verbose] [-d | --debug] [-e | --encoding &lt;encoding&gt;] &lt;input&gt; &lt;output&gt;\"<br \/>\nexit 1<br \/>\n}<br \/>\n<br \/>\nVERBOSE=false<br \/>\nENCODING=MACROMAN<br \/>\nDEBUG=false<br \/>\n<br \/>\nwhile true; do<br \/>\ncase $1 in<br \/>\n-v | --verbose) VERBOSE=true;;<br \/>\n-d | --debug) DEBUG=true;;<br \/>\n-*) echo \"Bad option $1\"; usage;;<br \/>\n*) break;;<br \/>\nesac<br \/>\nshift<br \/>\ndone<br \/>\n<br \/>\nSOURCE=\"$1\"<br \/>\nDESTINATION=\"$2\"<br \/>\n<br \/>\nif [ $DEBUG = true ]; then<br \/>\necho VERBOSE = $VERBOSE<br \/>\necho SOURCE = $SOURCE<br \/>\necho DESTINATION = \"$DESTINATION\"<br \/>\necho ENCODING = \"$ENCODING\"<br \/>\nexit<br \/>\nfi<br \/>\n<br \/>\nif [ \"x$SOURCE\" = x -o \"x$DESTINATION\" = x ]; then<br \/>\nusage;<br \/>\nfi<br \/>\n<br \/>\nconvert() {<br \/>\nINPUT=\"$1\"<br \/>\nOUTPUT=\"$2\"<br \/>\nFILENAME=`basename \"$INPUT\"`<br \/>\nif file \"$INPUT\" | grep Unicode ; then<br \/>\ncp \"$INPUT\" \"$OUTPUT\"<br \/>\n$VERBOSE &amp;&amp; echo \"Successfully copied $FILENAME\"<br \/>\nelse<br \/>\niconv -s -f $ENCODING -t UTF-8 \"$INPUT\" &gt;\"$OUTPUT\/$FILENAME\"<br \/>\n$VERBOSE &amp;&amp; echo \"Successfully converted $FILENAME\"<br \/>\nfi<br \/>\n}<br \/>\n<br \/>\nif [ -d \"$SOURCE\" ]; then<br \/>\nfor INPUT in \"$SOURCE\"\/* ; do<br \/>\nconvert \"$INPUT\" \"$DESTINATION\"<br \/>\ndone<br \/>\nelse<br \/>\nconvert \"$SOURCE\" \"$DESTINATION\"<br \/>\nfi<br \/>\n<br \/>\nexit<\/code><\/p>\n<p>Please excuse the poor tabbing due to wordpress. In any case, it worked and saved me a ton of time. All you need to do is copy this script into a file and make it executable. Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The other day at work, I needed to batch convert about one or two hundred files formatted in MACROMAN format to UTF-8. Well, it turns out there is a command line utility to do just this called iconv. I was very pleased when I found that because it was going to save me a lot &hellip; <a href=\"https:\/\/www.coastalvectors.com\/blog\/2011\/02\/utf-8-converter\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">UTF-8 Converter<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-452","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.coastalvectors.com\/blog\/wp-json\/wp\/v2\/posts\/452","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.coastalvectors.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.coastalvectors.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.coastalvectors.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.coastalvectors.com\/blog\/wp-json\/wp\/v2\/comments?post=452"}],"version-history":[{"count":27,"href":"https:\/\/www.coastalvectors.com\/blog\/wp-json\/wp\/v2\/posts\/452\/revisions"}],"predecessor-version":[{"id":1094,"href":"https:\/\/www.coastalvectors.com\/blog\/wp-json\/wp\/v2\/posts\/452\/revisions\/1094"}],"wp:attachment":[{"href":"https:\/\/www.coastalvectors.com\/blog\/wp-json\/wp\/v2\/media?parent=452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.coastalvectors.com\/blog\/wp-json\/wp\/v2\/categories?post=452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.coastalvectors.com\/blog\/wp-json\/wp\/v2\/tags?post=452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}