#!/bin/zsh # # Analyze the CR/NL-ness of a file # TMPF="/tmp/trat.$$.cranal" trap "/bin/rm -f $TMPF" EXIT function check_crnls { tr '@' ' ' < "$fin" | tr '\r' '@' > $TMPF count_crnls="$( grep -c '@$' $TMPF )" count_nlcrs="$( grep -c '^@' $TMPF )" # most likely a meaningless number # echo $count_crs, $count_nls, $count_crnls if (( count_crnls == count_nls )) ; then ftype=Windows echo $ftype: File "$fin" has $count_crnls CRNLs in it, and no loose CRs or NLs. else ftype=ProbablyWindows echo $ftype: File "$fin" has $count_crnls CRNLs\; but $count_crs CRs and $count_nls NLs. fi } fin="$1" flen=$( cat "$fin" | wc -c ) if (( flen == 0 )) ; then echo Zero-length file. fi count_crs=$( tr -cd '\r' < "$fin" | wc -c ) (( count_crs = count_crs + 0 )) # convert to numeric case "$count_crs" in 0) crs=N ;; *) crs=Y ;; esac count_nls=$( tr -cd '\n' < "$fin" | wc -c ) (( count_nls = count_nls + 0 )) # convert to numeric case "$count_nls" in 0) nls=N ;; *) nls=Y ;; esac case "$crs$nls" in NN) echo Unknown: File "$fin" has neither CRs nor NLs. ;; YN) echo Macintosh: File "$fin" has $count_crs CRs in it. ;; NY) echo Unix: File "$fin" has $count_nls NLS in it. ;; YY) if (( count_crs == count_nls )) ; then check_crnls else ftype=BinaryOrScrewedUp echo $ftype: File "$fin" has both CRs "($count_crs)" and NLs "($count_nls)" in it. fi ;; esac