Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

1 total

flip - newline conversion tool

export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
export IFS=$' \t\n'

# download & install flip
cd ~/Desktop
curl -L -O http://ccrma-www.stanford.edu/~craig/utility/flip/flip.osx
mv ~/Desktop/flip.osx ~/Desktop/flip
/usr/bin/sudo /bin/cp -ip ~/Desktop/flip /usr/bin
/usr/bin/sudo /usr/sbin/chown root:wheel /usr/bin/flip
/usr/bin/sudo /bin/chmod +x /usr/bin/flip
ls -l /usr/bin/flip

flip
flip -t /path/to/file     # display current file type
flip -u /path/to/file     # convert to Unix: "\n"
flip -d /path/to/file     # convert to MS-DOS/Windows: "\r\n"
flip -m /path/to/file     # convert to Macintosh (OS 9): "\r"


#----------------------------------


# create a test file

testfile="${HOME}/Desktop/testfile.txt"

function createfile() {
   testfile="${HOME}/Desktop/testfile.txt"
   /usr/bin/jot -b 'sample text' 10 | /bin/cat -n > "$testfile"
   # append a line with a '\r\n' line separator
   printf "%s\r\n\r\n\r\r\n" "sample text" >> "$testfile"      
   # append a last line without a terminating '\n'
   printf "%s" "sample text" >> "$testfile"          
   return 0
}


function odcfile() {

/usr/bin/od -A n -c < "$@" | /usr/bin/sed -E -e 's/^[[:space:]]{11}//' \
       -e s/[[:space:]]{4}/$'\001'/g \
       -e 's/[[:space:]]+//g' | \
       /usr/bin/tr -d '\n' | /usr/bin/tr '\001' ' ' | \
       /usr/bin/sed -e s/\\\\n/$'\\\\\\n\\\n'/g 

return 0
}


createfile
odcfile "$testfile"
flip -t "$testfile"     # display current file type

flip -d "$testfile"     # convert to MS-DOS/Windows: "\r\n"
flip -t "$testfile"
odcfile "$testfile"

flip -m "$testfile"     # convert to Macintosh (OS 9): "\r"
flip -t "$testfile"
odcfile "$testfile"

flip -u "$testfile"     # convert to Unix: "\n"
flip -t "$testfile"
odcfile "$testfile"
1 total