#!/bin/ksh ## ## SCRIPT: tr_linefeed_carrretn_unix_ms_mac ## ## Where: in $FEDIR/scripts where $FEDIR=/apps/nns_com/fea ## ######################################################################### ## PURPOSE: ## Converts text files between Unix, Microsoft, Mac --- ## by dealing with carriage-returns and linefeeds. ## ## NOTE: ## Lines in Microsoft text files end with BOTH carr-retn & linefeed. ## Lines in Unix text files end with linefeed ONLY. ## Lines in Mac text files end with carr-retn ONLY. ## ## ## NOTE on carriage-return (cr) char: ## An ASCII *CARRIAGE-RETURN* code is ## x'0D' (hexadecimal) ## \015 (octal) ## 13 (decimal) ## ## NOTE on line-feed (lf or new-line or nl) char: ## An ASCII *LINE-FEED* code is ## x'0A' (hexadecimal) ## \012 (octal) ## 10 (decimal) ## ## NOTE on form-feed (ff or new-page or np) char: ## An ASCII *FORM-FEED* code is ## x'0C' (hexadecimal) ## \014 (octal) ## 12 (decimal) ## ######################################################################### ## INPUT PARAMETERS: Three --- translation-type, file-in, file-out ## ## The translation type indicator is ## unix2ms, ms2unix, unix2mac, mac2unix, mac2ms, ms2mac ## ## Other special translation types: ## unix2doublespace ######################################################################### ## CALLED BY: Manually. ## ## Could eventually be called by a Tk-GUI 'wrapper' script ## called from a site SGI toolchest-drawer, like ## ## SGI Toolchest -> ## Handy Tools -> ## Office Tools ######################################################################### ## MAINTENANCE HISTORY: ## Written by: Blaise Montandon 26mar2004 Based on scripts like ## 'tr_0d_remove', 'catret', 'linefeed_tr' ## in $FEDIR/scripts ## See 'carr_ret_remove_bygui' ## for an example of a Tk-GUI wrapper script. ## Updated by: Blaise Montandon 26mar2004 ######################################################################### ########################################################## ## Check for proper number of args. ########################################################## if test \( "$1" = "" -o "$2" = "" -o "$3" = "" \) then echo " Script: $0 Error: REQUIRES 3 INPUT ARGUMENTS. Usage: $0 translate-from-to-indicator file-in file-out Indicators: unix2ms, ms2unix, unix2mac, mac2unix, mac2ms, ms2mac, unix2doublespace " exit fi ########################################################## ## Put args into vars. ########################################################## ## If there is a 4th arg with value TEST_YES, it is ## used below to set testing statements. ########################################################## FROM_TO="$1" FILEIN="$2" FILEOUT="$3" TEST_YN="$4" ############ ## unix2ms - Add carr-retn at linefeeds. ############ if test "$FROM_TO" = "unix2ms" then if test "$TEST_YN" = "TEST_YES" then set -x fi ######################################## ## Store carriage-return code (and line-feed) in var 'cr'. ## See 'man echo'. ######################################## cr=`echo '\015'` sed "s/$/$cr/g" $FILEIN > $FILEOUT ######################################## ## Could use unix 'to_dos' command, if available. ## ## 'to_dos' is a script that uses ## nawk '{printf("%s\r\n", $0);}' ## by "appending ^M at the end of each line" ## and ## by using ## echo '\c' >> ## to append "^Z at the end of the file". ## ## The echo "Sticks on the EOF character". ## The echo string contains '\032', the ## ASCII 'sub' character, before the '\c'. ## The '\c' probably provides a linefeed ## without a carriage-return. ## ## NOTE: In the 'nawk' the carriage-return ## goes BEFORE the line-feed. ######################################## ## References: TRY www.google.com on ## +sed +echo +"carriage return" +"new line" ######################################## if test "$TEST_YN" = "TEST_YES" then set - fi fi ############ ## ms2unix - Remove carr-retns (preferably at linefeeds only). ############ if test "$FROM_TO" = "ms2unix" then ######################################## ## Remove carr-retns everywhere. ## (They are almost always just at linefeeds. ## If this does not work in some cases, ## could try a 'sed' formulation.) ######################################## tr -d '\015' < $FILEIN > $FILEOUT ######################################## ## Could use unix 'to_unix' command, if available. ## 'to_unix' is actually a script that uses ## tr -d '\015\032' ## to strip out "all carriage-return and ctrl-Z's". ######################################## fi ############ ## unix2mac - Change linefeeds to carr-retns. ############ if test "$FROM_TO" = "unix2mac" then ######################################## ## Change linefeeds to carr-retns. ######################################## tr '\012' '\015' < $FILEIN > $FILEOUT fi ############ ## mac2unix - Change carr-retns to linefeeds. ############ if test "$FROM_TO" = "mac2unix" then ######################################## ## Change carr-retns to linefeeds. ######################################## tr '\015' '\012' < $FILEIN > $FILEOUT fi ############ ## mac2ms - Change carr-retns to linefeed-plus-carr-retn. ############ if test "$FROM_TO" = "mac2ms" then if test "$TEST_YN" = "TEST_YES" then set -x fi ######################################## ## USE 'mac2unix' then 'unix2ms' methods. WORKS! ######################################## cr=`echo '\015'` tr '\015' '\012' < $FILEIN | sed "s/$/$cr/g" > $FILEOUT ######################################## ## DOES NOT WORK: ######################################## # sed "s/$cr/\\n$cr/g" $FILEIN > $FILEOUT ######################################## ## COULD TRY: ######################################## # cr=`echo -n '\015'` # sed "s/$cr/$cr\\ # /" $FILEIN > $FILEOUT if test "$TEST_YN" = "TEST_YES" then set -x fi fi ########################################################### ## A FIRST 'mac2ms' ATTEMPT: NO GO. ## CANNOT FIND a 'cr' and 'lf' var method that works. ## The main problem may be protecting line-feed characters ## from interpretation by the shell, before 'sed' uses them. ## Might have to put the sed script in a file. ########################################################### ## NOTE the 'X' in 'mac2msX'. I.e. this option/argument # is deactivated, or just hidden. ########################################################### if test "$FROM_TO" = "mac2msX" then if test "$TEST_YN" = "TEST_YES" then set -x fi ######################################## ## Save carr-ret & linefeed chars in vars. ######################################## # cr=`echo '\015'` cr=`echo -n '\015'` # lf=`echo '\012'` lf=`echo -n '\012'` # lf=`echo 'LF'` if test "$TEST_YN" = "TEST_YES" then echo " sed input: s/${cr}/${cr}${lf}/g " fi ######################################## ## Add linefeed where there is a carr-retn. ######################################## # sed "s/$cr/$cr$lf/g" $FILEIN > $FILEOUT sed -e "s/${cr}/${cr}\ /g" $FILEIN > $FILEOUT if test "$TEST_YN" = "TEST_YES" then set - fi fi ############ ## ms2mac - Remove line-feeds (preferably at carr-retns.) ############ if test "$FROM_TO" = "ms2mac" then ######################################## ## Remove line-feeds everywhere. ## (They are almost always just at carr-retns. ## If this does not work in some cases, ## could try a 'sed' formulation.) ######################################## tr -d '\012' < $FILEIN > $FILEOUT fi ############## ## unixdoublespace - Add a linefeed at linefeeds. ############## if test "$FROM_TO" = "unix2doublespace" then if test "$TEST_YN" = "TEST_YES" then set -x fi ######################################## ## Add linefeed at linefeeds. ######################################## ## WORKS! ######################################## sed 's/$/\ /' $FILEIN > $FILEOUT if test "$TEST_YN" = "TEST_YES" then set - fi fi ######################################################## ## Write msg to stdout that the translate is done, and ## Show the input & output files. ######################################################## if test "$TEST_YN" = "TEST_YES" then echo " $FROM_TO is DONE." FEDIR="/apps/nns_com/fea" $FEDIR/scripts/shofil $FILEIN sleep 1 $FEDIR/scripts/shofil $FILEOUT fi