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!)

ffind - fuzzyfind from the current directory

# fuzzyfind from the current directory
function ffind() {
   declare regex filetype
   if [[ $# -gt 2 ]] || [[ $# -eq 0 ]]; then return 1; fi
   if [[ $# -eq 2 ]] && [[ "$1" == '-d' ]]; then filetype='d'; fi
   regex="$(printf "%s" "${!#}" | /usr/bin/sed -E -e 's/([^][]|\[[^][]+\])/\1\.\*/g' -e '/\.\*\$\.\*$/s/\.\*\$\.\*$/\$/')"
   regex="${PWD}/.*${regex}"
   /usr/bin/find -x "${PWD}" -type "${filetype:-f}" -iregex "$regex"
   return 0
}


ffind "searchstring"                # default is file search
ffind -d "[3-7]searchstring[5-9]"   # search for directories 
ffind "searchstring$"               # last character of file name is a "g"

str="searchstring"
str="[3-7]searchstring[5-9]"
str="search/string$"
printf "%s" "${str}" | /usr/bin/sed -E -e 's/([^][]|\[[^][]+\])/\1\.\*/g' -e '/\.\*\$\.\*$/s/\.\*\$\.\*$/\$/'

Store file information in SQLite database

A basic example of using BEGIN-INSERT-COMMIT to write data (gathered by a shell script) to an SQLite database file.


# basic form of BEGIN-INSERT-COMMIT to write data to existing db
/usr/bin/sqlite3 db  < <( 
cat <<-'EOF'
BEGIN;
insert statement 1;
insert statement 2;
insert statement 3;
...
COMMIT;
.q
EOF
)


man sqlite3

/usr/bin/sqlite3
sqlite> .help
sqlite> .q


# file info database function
# usage: finfodb [optional: -t] [directory] [optional: filename]

function finfodb() {

   declare -a ar
   declare -i i timestamp
   declare checksum db filename searchdir

   declare dbdir="${HOME}/Library/FileInfoDB" 
   declare dbname="finfodb"
   declare sleep_before_write="0.5"
   declare -i block_insert_num=1000

   dbdir="${dbdir%/}"    # cut off trailing slash if necessary

   if [[ $# -eq 0 ]]; then 

      printf "%s\n%s\n" 'No arguments given!' 'Usage: finfodb [optional: -t] [directory] [optional: filename]'
      return 1

   elif [[ "${1}" == '-t' ]]; then

      if [[ ! -d "${2}" ]]; then printf "%s\n" "No directory: ${2}"; return 1; fi
      timestamp=1
      searchdir="${2}"
      filename="${3:-*}"

   else

      if [[ ! -d "${1}" ]]; then printf "%s\n" "No directory: ${1}"; return 1; fi
      timestamp=0
      searchdir="${1}"
      filename="${2:-*}"

   fi


   # store the SQLite databases in $dbdir
   /bin/mkdir -p "${dbdir}"

   if [[ $timestamp -eq 1 ]]; then

      # create name of SQLite database with time stamp
      db="${dbdir}/${dbname}--$(/bin/date -u +%Y-%m-%d--%H.%M.%S--%Z)"
      #db="${dbdir}/${dbname}--$(/bin/date +%Y-%m-%d--%H.%M.%S--%Z)"

   else

      db="${dbdir}/${dbname}"

   fi


   # create the SQLite database if necessary

   if [[ ! -f "${db}" ]]; then

/usr/bin/sqlite3 "${db}"  < <( 
/bin/cat <<-'EOF'
     PRAGMA encoding = "UTF-8";
     PRAGMA auto_vacuum = 1;
     create TABLE filecheck (num INTEGER PRIMARY KEY, filepath TEXT, checksum TEXT, timeEnter DATE);
     CREATE TRIGGER insert_filecheck_timeEnter AFTER INSERT ON filecheck
     BEGIN
      UPDATE filecheck SET timeEnter = DATETIME('NOW') WHERE rowid = new.rowid;
     END;
EOF
)

   fi

   i=0
   ar[${i}]='BEGIN;'$'\n'

   while read -d $'\0' filepath; do 

      # write $block_insert_num insert statements to ${db}
      # empty $ar and reset $i to 0
      if [[ $i -ge $block_insert_num ]]; then
      #if [[ "${#ar[@]}" -ge $block_insert_num ]]; then
         let i++
         ar[${i}]='COMMIT;'$'\n'
         let i++
         ar[${i}]='.q'$'\n'
         /bin/sleep $sleep_before_write
         # write insert statements stored in $ar to $db (see below)
         # delete a possible single leading space in every line (i.e. array item) using "${ar[@]/# /}"
         /usr/bin/sqlite3 "${db}" < <( printf "%s" "${ar[@]/# /}" )
         #/usr/bin/sqlite3 "${db}" < <( for ((i=0; i < "${#ar[@]}"; i++)); do printf "%s" "${ar[$i]#"${ar[$i]%%[! ]*}"}"; done )
         #/usr/bin/sqlite3 "${db}" <<< "$(echo "${ar[@]}" | /usr/bin/sed 's/^ *//')"
         unset -v ar i
         declare -a ar
         declare -i i=0
         ar[${i}]='BEGIN;'$'\n'
         echo
      fi

      let i++

      filepath_cleaned="${filepath//[[:cntrl:]]/}"   # remove control characters such as \n, \r, ...

      # cf. http://codesnippets.joyent.com/posts/show/1395
      if [[ ${#filepath_cleaned} -lt 85 ]]; then
         printf "\r\e[0K\e[1;32m%s\e[0m  %s" "${i}" "${filepath_cleaned}"
      else
         printf "\r\e[0K\e[1;32m%s\e[0m  %s" "${i}" "${filepath_cleaned:0:40}.....${filepath_cleaned: -40}"
      fi

      checksum="$(/sbin/md5 -q "${filepath}" 2>/dev/null)"

      # sha512
      # sudo port install openssl
      # cf. http://trac.macports.org/wiki/InstallingMacPorts
      #checksum="$(/opt/local/bin/openssl dgst -sha512 2>/dev/null < "${filepath}")"

      # escape single quotes for SQLite; cf. http://sqlite.org/lang_expr.html
      filepath="${filepath//\'/''}" 

      # escape newline characters \n
      #filepath="${filepath//$'\n'/\\n}" 
    
      # remove control characters such as \n, \r, ...  
      #filepath="${filepath//[[:cntrl:]]/}"   

      # create insert statements
      ar[${i}]="insert into filecheck (filepath,checksum) values ('${filepath}', '${checksum}');"$'\n'

   done < <(/usr/bin/find -x "${searchdir}" \( -type f -or -type l \) -name "${filename}" -print0 2>/dev/null)

   echo

   let i++
   ar[${i}]='COMMIT;'$'\n'
   let i++
   ar[${i}]='.q'$'\n'

   /bin/sleep $sleep_before_write


   # write insert statements stored in $ar to $db
   # cf. Removing whitspace from variables in bash?, http://www.jlaforums.com/viewtopic.php?t=1427403
   #/usr/bin/sqlite3 "${db}" <<< "$(for ((i=0; i < "${#ar[@]}"; i++)); do printf "%s" "${ar[$i]#"${ar[$i]%%[! ]*}"}"; done)"
   #/usr/bin/sqlite3 "${db}" < <( for ((i=0; i < "${#ar[@]}"; i++)); do printf "%s" "${ar[$i]#"${ar[$i]%%[! ]*}"}"; done )

   # delete a possible single leading space in every line (i.e. array item) using "${ar[@]/# /}"
   /usr/bin/sqlite3 "${db}" < <( printf "%s" "${ar[@]/# /}" )

   # alternatives with sed to trim leading whitespace
   #/usr/bin/sqlite3 "${db}" <<< "$(echo "${ar[@]}" | /usr/bin/sed 's/^ *//')"
   #/usr/bin/sqlite3 "${db}"  < <(echo "${ar[@]}" | /usr/bin/sed 's/^ *//' )


   /usr/sbin/chown -R $(/usr/bin/logname):$(/usr/bin/logname) "${dbdir}"
   /bin/chmod -R 0700 "${dbdir}"

   return 0
}



finfodb
finfodb 1234
finfodb -t
finfodb /usr/bin
finfodb /usr/bin "*z*"
finfodb -t /usr/bin
finfodb -t /usr/bin "*z*"

finfodb ~/Library
finfodb /Library

open ~/Library/FileInfoDB

sqlite3 ~/Library/FileInfoDB/finfodb  ".dump"
sqlite3 ~/Library/FileInfoDB/finfodb  "select * from sqlite_master"
sqlite3 ~/Library/FileInfoDB/finfodb  "select * from filecheck";
sqlite3 ~/Library/FileInfoDB/finfodb  "select * from filecheck WHERE checksum LIKE ''";
sqlite3 ~/Library/FileInfoDB/finfodb  "select * from filecheck WHERE filepath LIKE '%sync%'";
sqlite3 ~/Library/FileInfoDB/finfodb  "select * from filecheck WHERE filepath LIKE '%/_sync'";
sqlite3 ~/Library/FileInfoDB/finfodb  "select * from filecheck WHERE filepath LIKE '%/ds%'";


sqlite3 ~/Library/FileInfoDB/finfodb  "select * from filecheck WHERE checksum LIKE ''";

sqlite3 ~/Library/FileInfoDB/finfodb  "delete from filecheck WHERE checksum LIKE ''";

sqlite3 ~/Library/FileInfoDB/finfodb  "select * from filecheck WHERE checksum LIKE ''";

sqlite3 ~/Library/FileInfoDB/finfodb "vacuum"

sqlite3 ~/Library/FileInfoDB/finfodb "pragma integrity_check"


Further information:

- man sqlite3
- SQLite Tutorial: Common Commands and Triggers
- Build an application using simple shell scripts and SQLite
- A Quick Guide to SQLite and Ruby
- SQLite: Available Documentation
- sqlite3: A command-line access program for SQLite databases
- Datatypes In SQLite Version 3
- The SQLite Query Optimizer Overview
- SQL As Understood By SQLite
- SQL As Understood By SQLite: BEGIN TRANSACTION
- SQL As Understood By SQLite: expression (LIKE operator)
- Atomic Commit In SQLite
- SQLite: PRAGMA command
- SQLite FAQ
- SQLite The Hammer
- SQLite ODBC Driver
- Actual ODBC Driver for Open Source Databases
- SQLite Download Page: Precompiled Binaries For Mac OS X

seticon - set icon of Mac OS X files

# See:
# http://osxutils.sourceforge.net
# http://www.sveinbjorn.org/osxutils_docs

cd ~/Desktop
curl -L -O http://surfnet.dl.sourceforge.net/sourceforge/osxutils/osxutils1.7.pkg.zip
unzip -qq osxutils1.7.pkg.zip
open -a Installer osxutils1.7.pkg

/bin/mkdir -p ~/Desktop/IconDir
/bin/cp /Library/Desktop\ Pictures/Nature{/Ladybug.jpg,'/Evening Reflections.jpg'} ~/Desktop/IconDir
/usr/bin/sips -i ~/Desktop/IconDir/*

/usr/bin/touch ~/Desktop/IconDir/testfile.txt
/bin/chmod 0777 ~/Desktop/IconDir/*

ls -l ~/Desktop/IconDir/*

open ~/Desktop/IconDir

# set icon
/usr/local/bin/seticon ~/Desktop/IconDir/Ladybug.jpg ~/Desktop/IconDir/testfile.txt

# update file system changes of open Finder window
/usr/local/bin/wsupdate ~/Desktop/IconDir/testfile.txt
#/bin/mv ~/Desktop/IconDir{/testfile.txt,/tmp.txt} && /bin/mv ~/Desktop/IconDir{/tmp.txt,/testfile.txt} 


# set icon
/usr/local/bin/seticon ~/Desktop/IconDir/'Evening Reflections'.jpg ~/Desktop/IconDir
/usr/local/bin/wsupdate ~/Desktop/IconDir

/bin/chmod -R 0755 ~/Desktop/IconDir
ls -al ~/Desktop/IconDir/*


# geticon
sips -i file.jpg
geticon -t icns file.jpg


# list files with resource forks
# cf. http://www.entropy.ch/blog/Mac+OS+X/2005/03/30/,
# http://forums.macosxhints.com/showthread.php?t=70224 and
# "can UNIX resolve OS X aliases?", http://forums.macosxhints.com/showthread.php?t=19960

cd ~/Desktop/IconDir
ls -1 | while IFS= read -r i; do if [[ -s ${i}/..namedfork/rsrc ]]; then ls -l "${i}/..namedfork/rsrc"; fi; done
find . -mindepth 1 -maxdepth 1 -type f -exec test -s {}/..namedfork/rsrc \; -print
find . -mindepth 1 -maxdepth 1 -type f -exec test -s {}/..namedfork/rsrc \; -print0 | xargs -0 -n1 -I '{}' ls -l '{}'/..namedfork/rsrc
find . -mindepth 1 -maxdepth 1 -type f -exec test -s {}/..namedfork/rsrc \; -print0 | xargs -0 -n1 -I '{}' sed -n -e 'l' '{}'/..namedfork/rsrc
find . -mindepth 1 -maxdepth 1 -type f -exec test -s {}/..namedfork/rsrc \; -print0 | xargs -0 -n1 -I '{}' ruby -n -e 'p $_.to_s' '{}'/..namedfork/rsrc
find . -mindepth 1 -maxdepth 1 -type f -exec test -s {}/..namedfork/rsrc \; -print0 | xargs -0 -n1 -I '{}' strings '{}'/..namedfork/rsrc

# delete resource fork
find . -type f -maxdepth 1 -exec test -s {}/..namedfork/rsrc \; -print0 | xargs -0 -I '{}' /bin/cp /dev/null '{}/..namedfork/rsrc'

ditto --norsrc file.jpg{,.bak}
ditto --norsrc file.jpg.bak file.jpg

# some additional resource fork tools
man Rez
man DeRez
man RezWack     # create a flattened file from resource and data fork
man UnRezWack
man SplitForks
man FixupResourceForks

/Developer/Tools/Rez --help
/Developer/Tools/DeRez --help
/Developer/Tools/RezWack --help
/Developer/Tools/UnRezWack --help
/Developer/Tools/SplitForks --help
/System/Library/CoreServices/FixupResourceForks --help

ls -1 /Developer/Tools/*
/Developer/Tools/RezDet --help
/Developer/Tools/RezDet -d file; echo $?
/Developer/Tools/RezDet -s file; echo $?

man CpMac | less -N -j -9 -p 'As of Mac OS X 10.4' 
man MvMac | less -N -j -11 -p 'As of Mac OS X 10.4' 

# http://tclresource.sourceforge.net
man tclresource


Further information:

- Mac 101: Change Your Icons
- Mac OS X: Changing the Icon for a File Type
- How Do I Change File Icons And The Default Application On My Mac?
- Set Icon
- Mac Icon FAQ
- Setting an Icon From the CLI
- osxutils Documentation
- Aliases, symbolic links, Path Finder aliases... Help!
- BlueHarvest
- Making Resource-Fork-Aware Backups with rsync on Mac OS X

fsinfo

fsinfo - get file status information with Apple's FSMegaInfo sample code
See: FSMegaInfo by Apple and FSMegaInfoGUI by Ross Tulloch

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

/usr/bin/sudo /bin/mkdir -p /usr/local/bin
/usr/bin/sudo /usr/sbin/chown root:wheel /usr/local /usr/local/bin
/usr/bin/sudo /bin/chmod 0755 /usr/local /usr/local/bin

cd ~/Desktop
curl -LO http://developer.apple.com/samplecode/FSMegaInfo/FSMegaInfo.zip
unzip -qq FSMegaInfo.zip

open -e ~/Desktop/FSMegaInfo/Read\ Me\ About\ FSMegaInfo.txt


file -ik ~/Desktop/FSMegaInfo/build/Debug/FSMegaInfo
otool -Lmv $_
otool -hmVv $_
otool64 -hmVv $_
lipo -detailed_info $_


/usr/bin/sudo /bin/cp -i ~/Desktop/FSMegaInfo/build/Debug/FSMegaInfo /usr/local/bin
ls -l /usr/local/bin/FSMegaInfo

/usr/bin/sudo /bin/ln -is /usr/local/bin/FSMegaInfo /usr/local/bin/fsinfo
ls -l /usr/local/bin/fsinfo


# create a test file
testfile="${HOME}/Desktop/testfile.txt"
jot -b 'sample text' 10 | cat -n > $testfile


FSMegaInfo help
fsinfo help
fsinfo help 2>&1 | grep -i info

fsinfo -v help stat
fsinfo stat $testfile

fsinfo -v help FSGetCatalogInfo
fsinfo -v help FSGetCatalogInfo 2>&1 | grep -i finder
fsinfo -v FSGetCatalogInfo $testfile
fsinfo -v FSGetCatalogInfo -kFSCatInfoGettableInfo $testfile
fsinfo -v FSGetCatalogInfo -kFSCatInfoGettableInfo $testfile 2>&1 | grep -i encod


# createDate

fsinfo -v FSGetCatalogInfo -kFSCatInfoCreateDate $testfile
fsinfo -v FSGetCatalogInfo -kFSCatInfoAccessDate,kFSCatInfoContentMod,kFSCatInfoAttrMod,kFSCatInfoCreateDate $testfile

stat -x $testfile | tail -n 3
stat -f $'%N:\nlast accessed:\t\t%Sa\nlast modified:\t\t%Sm\nlast inode change:\t%Sc' $testfile

touch -f $testfile

stat -x $testfile | tail -n 3
stat -f $'%N:\nlast accessed:\t\t%Sa\nlast modified:\t\t%Sm\nlast inode change:\t%Sc' $testfile

fsinfo -v FSGetCatalogInfo -kFSCatInfoCreateDate $testfile
fsinfo -v FSGetCatalogInfo -kFSCatInfoAccessDate,kFSCatInfoContentMod,kFSCatInfoAttrMod,kFSCatInfoCreateDate $testfile

cwd - copy with date

unset -f cwd
function cwd() {
   declare dirname filename newfile
   if [[ ! -f "$1" ]]; then echo "No such file: ${1}"; return 1; fi
   if [[ $# -eq 1 ]]; then 
      dirname="$(/usr/bin/dirname "${1}")"
   elif [[ $# -eq 2 ]]; then
      if [[ ! -d "$2" ]]; then echo "No such directory: ${2}"; return 1; fi
      dirname="${2%/}"
   else
      echo "argument error"
      return 1
   fi
   #/bin/sleep 1
   filename="$(/usr/bin/basename "${1}")"
   newfile="${dirname}/${filename}.$(/bin/date +%Y-%m-%d-%H.%M.%S)"
   #newfile="${dirname}/${filename}.$(/bin/date +%Z-%Y-%m-%d-%H.%M.%S)"
   /bin/cp -ip "${1}" "${newfile}"
   return 0
}


cwd file
cwd file dir

Counting lines

# create a test file
testfile="${HOME}/Desktop/testfile.txt"
jot -b 'sample text' 10 | cat -n > "$testfile"
printf "%s\n" >> "$testfile"                                 # add an empty line
printf "%s\n\r\n\r\n\r\r\n" "sample text" >> "$testfile"     # add lines with '\r\n' as line separators 
printf "%s" "sample text" >> "$testfile"                     # add a last line without a terminating '\n'       

open -e "$testfile"

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
}

odcfile "$testfile"



# wc -l
wc -l < "$testfile"     # returns the number of '\n' characters

# ed 
ed -s "$testfile" <<< '='     # all lines

# cat
cat -n "$testfile" | awk 'END {print $1}'   # all lines


# grep
grep -c $'\n' "$testfile"                 # all lines
grep -c '^.*$' "$testfile"                # all lines
#grep -c $'\000' "$testfile"
grep -c $'\r$' "$testfile"                # all lines ending with \r\n
grep -c '^[[:space:]]*$' "$testfile"      # all blank lines
grep -cv '^[[:space:]]*$' "$testfile"     # all non-blank lines


# sed
sed -n '$=' "$testfile"                          # all lines
sed -n /$'\r'$/= "$testfile" | wc -l             # all lines ending with \r\n
sed -n '/^[[:space:]]*$/=' "$testfile" | wc -l   # all blank lines
sed -n '/[^[:space:]]/=' "$testfile" | wc -l     # all non-blank lines


# awk
awk 'END {print NR}' "$testfile"                           # all lines
awk '{x++} END {print x}' "$testfile"                      # all lines
awk '/^.*$/ {++x} END {print x}' "$testfile"               # all lines
awk '/\r$/ {++x} END {print x}' "$testfile"                # all lines ending with \r\n
awk '/^[[:space:]]*$/ {++x} END {print x}' "$testfile"     # all blank lines
awk '/[^[:space:]]/ {++x} END {print x}' "$testfile"       # all non-blank lines


# nl
nl "$testfile" | awk 'END {print $1}'
nl -b a "$testfile" | awk 'END {print $1}'   # including empty lines


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


# counting the lines of files in a directory

DIR=/path/to/dir

find "$DIR" -type f -name "*.txt" -print0 | xargs -0 wc -l

find "$DIR" -type f -name "*.txt" -print0 | xargs -0 sed -n '$='

find "$DIR" -type f -name "*.txt" -print0 | xargs -0 awk 'END {print NR}'
find "$DIR" -type f -name "*.txt" -print0 | xargs -0 awk '{x++} END {print x}'
find "$DIR" -type f -name "*.txt" -print0 | xargs -0 awk '/\r$/ {++x} END {print x}'
find "$DIR" -type f -name "*.txt" -print0 | xargs -0 awk '/^[[:space:]]*$/ {++x} END {print x}'
find "$DIR" -type f -name "*.txt" -print0 | xargs -0 awk '/[^[:space:]]/ {++x} END {print x}'


# rather slow, but without the "Argument list too long" issue: /usr/sbin/sysctl kern.argmax
# cf. http://www.onlamp.com/pub/a/bsd/2002/03/14/FreeBSD_Basics.html

declare -i linecnt=0
while read -d $'\0' file; do
   #linecnt=$((${linecnt} + $(/usr/bin/sed -n '$=' "${file}")))
   let "linecnt += $(/usr/bin/sed -n '$=' "${file}")"   # cf. help let
done < <(/usr/bin/find "$DIR" -type f -name "*.txt" -print0)

echo $linecnt

Delete carriage returns & newlines with sed

# delete newlines

printf "a\nb\nc\nd\ne\nf" | sed -n -e 'l'
printf "a\nb\nc\nd\ne\nf" | sed -E -e :a -e '$!N; s/\n//g; ta'

printf "a\n\nb" | sed -n -e 'l'
printf "a\n\nb" | sed -E -e :a -e '$!N; s/\n//g; ta' | sed -n -e 'l'
printf "a\n\nb" | sed -E -e :a -e '$!N; s/\n$//g; ta' | sed -n -e 'l'
printf "a\n\n\n\n\n\nb" | sed -E -e :a -e '$!N; s/\n$//g; ta' | sed -n -e 'l'


# delete carriage returns

printf "he\r\rllo\n" | sed -n -e 'l'
printf "he\r\rllo\n" | sed -e s/$'\r'//g | sed -n -e 'l'
printf "he\r\rllo\n" | sed -e s/$'\r\r'/$'\r'/g | sed -n -e 'l'

CR=$'\r'
printf "hello\r\r\n" | sed "s/$CR$CR$/$CR/g" | sed -n -e 'l'


# alternatives

printf "a\nb\nc\nd\ne\nf" | tr -d '\n'

# cf. http://linux.dsplabs.com.au/rmnl-remove-new-line-characters-tr-awk-perl-sed-c-cpp-bash-python-xargs-ghc-ghci-haskell-sam-ssam-p65/
while read -d $'\n'; do echo -n "${REPLY} "; done < <((printf "a\nb\nc\nd\ne\nf"))
while read -d $'\n'; do printf "%s   " "${REPLY}"; done < <((printf "a\nb\nc\nd\ne\nf"))

xargs echo < <((printf "a\nb\nc\nd\ne\nf"))
xargs printf "%s " < <((printf "a\nb\nc\nd\ne\nf"))

printf "a\nb\nc\nd\ne\nf" | /usr/bin/paste -s -d ' ' -     # cf. man paste

# delete newlines in-place (also see below)
vim -e -s +':%j' +'w!' +'qa!' /path/to/file

# insert newlines again
printf "%s\n" "hello" | sed -e s/l/$'\\\n'/g | sed -n -e 'l'
printf "%s\n" "hello" | sed -E s/\(l\)/\\1$'\\\n'/g | sed -n -e 'l'


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


# converting between \n and \n\r

# cf. also flip, http://ccrma.stanford.edu/~craig/utility/flip/ and 
# http://codesnippets.joyent.com/posts/show/1660


# convert \n into \r\n
printf "a\nb\nc\nd\ne\nf\n"
printf "a\nb\nc\nd\ne\nf\n" | sed -n -e 'l'

printf "a\nb\nc\nd\ne\nf\n" | sed s/$/$'\r'/
printf "a\nb\nc\nd\ne\nf\n" | sed s/$/$'\r'/ | sed -n -e 'l'

printf "a\nb\n\n\n\n\n\n\n\n\n\n\nc\nd\ne\nf\n" | sed -E -e :a -e '$!N; s/\n$//g; ta' | sed -E s/$/$'\r'/ | sed -n -e 'l'

while read -d $'\n'; do printf "%s\r\n" "${REPLY}"; done < <((printf "a\nb\nc\nd\ne\nf\n"))
while read -d $'\n'; do printf "%s\r\n" "${REPLY}"; done < <((printf "a\nb\nc\nd\ne\nf\n")) | sed -n -e 'l'


# convert \r\n into \n
printf "a\r\nb\r\nc\r\nd\r\ne\r\nf\r\n"
printf "a\r\nb\r\nc\r\nd\r\ne\r\nf\r\n" | sed -n -e 'l'

printf "a\r\nb\r\nc\r\nd\r\ne\r\nf\r\n" | sed s/$'\r'$//
printf "a\r\nb\r\nc\r\nd\r\ne\r\nf\r\n" | sed s/$'\r'$// | sed -n -e 'l'

printf "a\r\nb\r\r\r\r\r\r\r\r\r\r\nc\r\nd\r\ne\r\nf\r\n" | sed s/$'\r'*$// | sed -n -e 'l'
printf "a\r\nb\r\r\r\r\r\r\r\r\r\r\nc\r\nd\r\ne\r\nf\r\n" | sed -E s/$'\r'+$// | sed -n -e 'l'

# lacks a terminating \n though
while read -d $'\r'; do printf "%s" "${REPLY}"; done < <((printf "a\r\nb\r\nc\r\nd\r\ne\r\nf\r\n"))
while read -d $'\r'; do printf "%s" "${REPLY}"; done < <((printf "a\r\nb\r\nc\r\nd\r\ne\r\nf\r\n")) | sed -n -e 'l'


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


# create test files

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

function createfiles() {

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

/usr/bin/touch "$output"

#/usr/bin/jot -b 'sample text' 10 | /bin/cat > "$testfile"
/usr/bin/jot -b 'sample text' 10 | /bin/cat -n > "$testfile"
printf "%s\r\n\r\n\r\r\n" "sample text" >> "$testfile"      # append a line with a '\r\n' line separator
printf "%s" "sample text" >> "$testfile"          # append a last line without a terminating '\n'

return 0
}

createfiles


# inspect test file

cat -vet "$testfile"
ed -s "$testfile" <<< $',l'
sed -n -e 'l'  "$testfile"
ruby -n -e 'p $_.to_s' < "$testfile"


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
}


odcfile "$testfile"


# remove newlines "\n"

# create a new file with newline characters replaced with a space
sed -e :a -e '$!N; s/\n/ /g; ta' "$testfile" > "$output"
odcfile "$output"

# same, but in-place
sed -i "" -e :a -e '$!N; s/\n/ /g; ta' "$testfile"
odcfile "$testfile"

createfiles

# delete newlines in-place
sed -i "" -e :a -e '$!N; s/\n//g; ta' "$testfile"
odcfile "$testfile"

createfiles

# alternative for creating a new file without newlines
tr -d '\n' < "$testfile" > "$output"
odcfile "$output"



# delete carriage returns "\r"

# create a new file with carriage returns deleted
sed -e s/$'\r'//g "$testfile" > "$output"
sed -e s/$'\r'$//g "$testfile" > "$output"  # only at line end
odcfile "$output"

# alternative
tr -d '\r' < "$testfile" > "$output"
odcfile "$output"


# delete carriage returns in-place

sed -i "" -e s/$'\r'//g "$testfile" > "$output"
odcfile "$testfile"

createfiles

# cf. http://bash-hackers.org/wiki/doku.php?id=howto:edit-ed (Pitfalls)
ed -s "$testfile" <<< $'H\n,g/\r/s/\r//\n,w'      # delete one carriage return in a line
ed -s "$testfile" <<< $'H\n,g/\r/s/\r//g\n,w'     # delete all carriage returns in a line
ed -s "$testfile" <<< $'H\n,g/\r$/s/\r$//g\n,w'   # delete a carriage return at line end

odcfile "$testfile"



printf "\n" | od -A n -c
printf "\012" | od -A n -c
printf "\x0a" | od -A n -c

printf '[ctrl-v][ctrl-j]' | od -A n -c      #  \n or ^J


printf "\r" | od -A n -c
printf "\015" | od -A n -c
printf "\x0d" | od -A n -c

printf '[ctrl-v][ctrl-j]' | od -A n -c      #  \r or ^M


# vim
createfiles

vim "$testfile"
#...
:set number
:set list
:%s/^M//g
:set fileformat=unix
#:set fileformat=dos
:x

odcfile "$testfile"


# edit files in-place with vim

createfiles

# delete newlines in-place
vim -e -s +':%j' +'w!' +'qa!' "$testfile"
odcfile "$output"

createfiles

# replace every \r with \n
# cf. http://vim.wikia.com/wiki/Change_end-of-line_format_for_dos-mac-unix
vim -e -s +':%s/\r/\r/g' +':set fileformat=unix' +'w!' +'qa!' "$testfile"
odcfile "$output"

createfiles

# delete every \r
vim -e -s +':%s/\r//g' +':set fileformat=unix' +'w!' +'qa!' "$testfile"
odcfile "$output"

createfiles

# delete \r only when it occurs at the end of a line
vim -e -s +':%s/\r$//g' +':set fileformat=unix' +'w!' +'qa!' "$testfile"
odcfile "$output"

createfiles

# replace carriage return line endings with \n
# cf. Getting rid of ^M - mixing dos and unix, http://www.vim.org/tips/tip.php?tip_id=26
printf "\r" >> "$testfile"
vim -e -s +':g/\r$/s///g' +':set fileformat=unix' +'w!' +'qa!' "$testfile"
odcfile "$testfile"

createfiles

# delete last \n of file
vim -e -s +':set noeol bin' +':set fileformat=unix' +'w!' +'qa!' "$testfile"
odcfile "$output"

createfiles

# change mixed mode files to DOS mode
#vim -e -s +':%s/\r\r/\r/g' +'w!' +'qa!' "$testfile"
sed -i '' -e s/$'\r\r'/$'\r'/g  "$testfile"
vim -e -s +':e ++ff=dos' +'w!' +'qa!' "$testfile"
vim -e -s +':e ++ff=dos' +':set ff=dos' +'w!' +'qa!' "$testfile"
odcfile "$output"

vim "$testfile"
:set ff?
:x

open or cd to the directory of a file path

# open directory of a file path
# can be used together with the [esc-.] key sequence
function odf() { /usr/bin/open "$(/usr/bin/dirname "$@")"; return 0; }

# cd to the directory of a file path
function cdf() { cd "$(/usr/bin/dirname "$@")"; return 0; }


ls -l /Library/Desktop\ Pictures/Nature/Tranquil\ Surface.jpg
ls -l /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ExecutableBinaryIcon.icns 

odf [esc-.]
cdf [esc-.]


odf /System/Library/SystemProfiler/SPFirewallReporter.spreporter/Contents/Resources/
cdf /System/Library/SystemProfiler/SPFirewallReporter.spreporter/Contents/Resources/English.lproj/Localizable.strings

Changing the Finder "Open With" contextual menu from the command line

The command line tool duti (http://duti.sourceforge.net) can be used to change default file-application launching associations as shown by the Finder "Open With" contextual menu. Changing the Finder "Open With" contextual menu means changing the default way we "open" documents on the command line as well.

The "lsregister" command can be used to dump or rebuild the Launch Services database.

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


# download & install duti, MacVim and TextMate

# duti - set default UTI handlers
# cf. http://duti.sourceforge.net/documentation.php and
# http://developer.apple.com/macosx/uniformtypeidentifiers.html

cd ~/Desktop
curl -L -O http://downloads.sourceforge.net/duti/duti-1.3.0.pkg.tar.gz
tar -xzf duti-1.3.0.pkg.tar.gz
open -a Installer duti-1.3.0.pkg
stat -x /usr/local/bin/duti
duti -h
man duti


# MacVim, http://code.google.com/p/macvim/

cd ~/Desktop
curl -L -O http://macvim.googlecode.com/files/MacVim-7_2-stable-1_2.tbz
tar -xjf MacVim-7_2-stable-1_2.tbz
#open MacVim-7_2-stable-1_2.tbz    # uses BOMArchiveHelper (10.4) or Archive Utility (10.5) respectively
mv -i MacVim-7_2-stable-1_2 MacVim
mv -i ~/Desktop/MacVim /Applications


# TextMate, http://macromates.com

cd ~/Desktop
curl -L -O http://macromates.com/textmate/files/TextMate_1.5.7.dmg
hdiutil mount ~/Desktop/TextMate_1.5.7.dmg
cp -Ri '/Volumes/TextMate 1.5.7/TextMate.app' /Applications
hdiutil unmount '/Volumes/TextMate 1.5.7'
#/usr/bin/sudo /bin/ln -s /Applications/TextMate.app/Contents/Resources/mate /usr/local/bin/mate


# get the path to the "lsregister" command
locate lsregister | head -n 1
find /System/Library/Frameworks -type f -name lsregister -ls
find /System/Library/Frameworks/CoreServices.framework -type f -name lsregister -ls

alias lsregister="/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"

#/usr/bin/sudo /bin/ln -s `locate lsregister | head -n 1` /bin/lsregister   # alternative

# rebuild Launch Services database
#alias rlsdb='lsregister -kill -r -f -all system,local,user'    # Mac OS X 10.5; cf. http://9stmaryrd.com/2008/05/16/158
alias rlsdb='lsregister -kill -r -f -domain local -domain system -domain user'

rlsdb

lsregister -h

lsregister -dump | grep -i duti
lsregister -dump | grep -i MacVim
lsregister -dump | grep -i TextMate
lsregister -dump | grep -i TextEdit
lsregister -dump | grep -i Safari
lsregister -dump | grep -i helpviewer


lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*)$/  \1/p' | sort | uniq | nl
lsregister -dump | grep '[[:space:]]uti:' | awk '{ print $2 }' | sort | uniq | nl
lsregister -dump | sed -E -n -e 's/^.*identifier:[[:space:]]+([^[:space:]].*)$/  \1/p' | sort | uniq | nl
lsregister -dump | sed -E -n -e 's/^.*path:[[:space:]]+([^[:space:]].*\.app)$/  \1/p' | sort | uniq | nl


lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*)$/  \1/p' | nl | egrep -i 'duti|MacVim|TextMate|TextEdit|Safari|helpviewer'
lsregister -dump | sed -E -n -e 's/^.*identifier:[[:space:]]+([^[:space:]].*)$/  \1/p' | nl | egrep -i 'duti|MacVim|TextMate|TextEdit|Safari|helpviewer'
lsregister -dump | sed -E -n -e 's/^.*path:[[:space:]]+([^[:space:]].*\.app)$/  \1/p' | nl | egrep -i 'duti|MacVim|TextMate|TextEdit|Safari|helpviewer'


lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(public\..*)$/  \1/p' | sort | uniq | nl
lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*text.*)$/  \1/p' | sort | uniq | nl


alias utis="lsregister -dump | grep '[[:space:]]uti:' | awk '{ print \$2 }' | sort | uniq"
alias utis="lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*)$/\1/p' | sort | uniq"
utis

utis | grep -i 'public\.' | nl

defaults read com.apple.LaunchServices


# ... or just use wsupdate from http://osxutils.sourceforge.net 
# move & copy the same file
unset -f mc
function mc() {
   if [[ $# -gt 1 ]] || [[ ! -e "$@" ]]; then return 1; fi
   tmpfile="${@}.tmp-${RANDOM}"
   printf "%s\n" "$tmpfile"
   /bin/mv "$@" "${tmpfile}"
   /bin/sleep 0.2
   /bin/cp -p "${tmpfile}" "$@"
   /bin/rm "${tmpfile}"
   return 0
}


unset -f mc
function mc() {
   if [[ $# -gt 1 ]] || [[ ! -e "$@" ]]; then return 1; fi
   tmpfile="/tmp/tmpfile.tmp-${RANDOM}"
   printf "%s\n" "$tmpfile"
   /bin/mv "$@" "${tmpfile}"
   /bin/sleep 0.2
   /bin/cp -Rp "${tmpfile}" "$@"
   /bin/rm -Rf "${tmpfile}"
   return 0
}



echo hello world > ~/Desktop/test.txt
echo hello world > ~/Desktop/test.html


# switch between Safari & Help Viewer as the default launching app for html files
open ~/Desktop/test.html


# cf. http://duti.sourceforge.net/documentation.php

printf "%s\n" 'com.apple.helpviewer   public.html   all' | duti     
#mc ~/Desktop/test.html
wsupdate ~/Desktop/test.html
open ~/Desktop/test.html

printf "%s\n" 'com.apple.Safari   public.html   all' | duti
wsupdate ~/Desktop/test.html
open ~/Desktop/test.html

rlsdb


# switch between TextEdit, MacVim & TextMate as the default launching app for plain text files

open ~/Desktop/test.txt

printf "%s\n" 'com.macromates.textmate   public.plain-text   all' | duti
open ~/Desktop/test.txt
wsupdate ~/Desktop/test.txt

printf "%s\n" 'org.vim.MacVim   public.plain-text   all' | duti
open ~/Desktop/test.txt    # type: ":q" or ":x" to quit test.txt
wsupdate ~/Desktop/test.txt

printf "%s\n" 'com.apple.TextEdit   public.plain-text   all' | duti
open ~/Desktop/test.txt
wsupdate ~/Desktop/test.txt



# cf. http://duti.sourceforge.net/documentation.php

/bin/cat > ~/Desktop/test.duti <<-'EOF'
com.apple.helpviewer   public.html   all
com.macromates.textmate   public.plain-text   all
EOF

# remove white space at line beginning
ed -s ~/Desktop/test.duti <<< $',s/^[[:space:]]*//\nw'

open -e ~/Desktop/test.duti

duti -v ~/Desktop/test.duti

wsupdate ~/Desktop/test.html
open ~/Desktop/test.html

wsupdate ~/Desktop/test.txt
open ~/Desktop/test.txt


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


# GUI alternative to switch the default launching app for a single file
[right-click on a file] + [alt]  ->  "Always Open With"

# GUI alternative to change the default launching app for all files of the same kind
[right-click on a file] -> "Get Info" -> "Open with:" -> [select an app] -> "Change All ..."

"openterminal" contextual menu item with Automator

Right-click on a file or folder in a Finder window and select Automator -> openterminal to open it in Terminal.app.
(i.e. cd to the folder or open the file in the nano text editor)

open -a Automator

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

Drag or add actions here to build your workflow:
Library: Finder -> Action: Get Selected Finder Items
Library: Automator -> Action: Run Shell Script
                                 - Shell: /bin/sh
                                 - Pass input: as arguments


if [[ $# -gt 1 ]]; then exit 0; fi

if [[ -d "$@" ]]; then

   printf "%s" "$@" | /usr/bin/pbcopy
   #/usr/local/bin/cpath # cf. http://osxutils.sourceforge.net

   /usr/bin/open -a Terminal

   /usr/bin/osascript -e 'tell application "Terminal" to do script with command "printf \"\\e[8;26;115;t\"; printf \"\\e[3;300;240;t\"; cd \"$(/usr/bin/pbpaste)\"; /usr/bin/clear" in (first window whose name contains " ")'

# alternative without /usr/bin/clear (experimental)
# requires:
# defaults write com.apple.Terminal Autowrap NO
# or:
# Terminal menu -> Window Settings ...  -> Buffer -> in the Scrollback 
# section check the box next to "Wrap lines that are too long" 
# -> click "Use Settings as Defaults"

#   /usr/bin/osascript -e 'tell application "Terminal" to do script with command "cd \"$(/usr/bin/pbpaste)\"; /usr/bin/tput cup 0 0; /usr/bin/tput dl1; /usr/bin/tput el; /usr/bin/tput dl1; /usr/bin/tput el" in (first window whose name contains " ")'

   exit 0

elif [[ -r "$@" ]]; then 

   printf "%s"