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

iregex

function iregex() {
   declare escaped_str iregexstr lower upper
   escaped_str="$(printf "%q" "${1}")"
   iregexstr=
   lower="$(printf "%s" "${escaped_str}" | /usr/bin/tr '[[:upper:]]' '[[:lower:]]')"
   upper="$(printf "%s" "${escaped_str}" | /usr/bin/tr '[[:lower:]]' '[[:upper:]]')"

   for ((i=0; i < "${#lower}"; i++)); do
      iregexstr="${iregexstr}[${lower:${i}:1}${upper:${i}:1}]"
   done

   printf "%s" "${iregexstr}" | /usr/bin/sed -E -e 's/\[([[:space:]])[[:space:]]\]/\1/g'  -e 's/\[([[:punct:]])[[:punct:]]\]/\1/g'
   return 0
}


iregex 'hello \ | / again!?'
re="$(iregex 'hello \ | / again!?')"


# replace the following string
string='helLo \ | / again!?'

re_str='hello \ | / again!?'
iregex "${re_str}"
regex="$(iregex "${re_str}")"   # always use iregex "${re_str}"
echo "${regex}"

echo "${string}" | sed "s/${regex}/replacement worked/g"   # / not escaped in regex
echo "${string}" | sed "s|${regex}|replacement worked|g"   # always use "s|${re}|replacement|"


# note: printf does not escape / to \/ in function iregex above
#printf "%q\n" "/|\ "
printf "%q\n" '/|\'   


# cf. gnused, http://codesnippets.joyent.com/posts/show/1835
string='helLo \ | / again!?'
echo "${string}" | gnused 's/hello \\ | \/ again!?/replacement worked/ig'
echo "${string}" | gnused 's|hello \\ \| / again!?|replacement worked|ig'


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


# replace the following string containing a tab character \t
string=$'helLo \ | / \tagain!?'
echo "${string}"
iregex "${string}"


re_str1='hello \ | / '
re_str2=$'\t'
re_str3='again!?'

re1="$(iregex "${re_str1}")"
re2="${re_str2}"
re3="$(iregex "${re_str3}")"

echo "${string}" | sed "s|${re1}${re2}${re3}|replacement worked|g"

echo "${string}" | gnused 's|hello \\ \| / \tagain!?|replacement worked|ig'


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


TAB=$'\t'

echo $'abc\tdef'
echo $'abc\tdef' | sed $'s/\t//'
echo $'abc\tdef' | sed s/$'\t'//
echo $'abc\tdef' | sed "s/${TAB}//"

echo $'abc\tdef' | sed $'s/\t/TAB/'
echo $'abc\tdef' | sed $'s/\t/TAB/' | sed $'s/TAB/\t/'
echo $'abc\tdef' | sed $'s/\t/TAB/' | sed s/TAB/$'\t'/
echo $'abc\tdef' | sed $'s/\t/TAB/' | sed "s/TAB/${TAB}/"


# gnused

echo $'abc\tdef' | sed 's/\t/TAB/'
echo $'abc\tdef' | gnused 's/\t/TAB/'

echo $'abc\tdef' | gnused $'s/\t/TAB/'
echo $'abc\tdef' | gnused "s/${TAB}/TAB/"


echo $'abc\tdef' | gnused $'s/\t/TAB/' | gnused $'s/TAB/\t/'
echo $'abc\tdef' | gnused $'s/\t/TAB/' | gnused s/TAB/$'\t'/
echo $'abc\tdef' | gnused $'s/\t/TAB/' | gnused "s/TAB/${TAB}/"

echo $'abc\tdef' | gnused 's/\t/TAB/' | gnused 's/TAB/\t/'

Compiling GNU sed 4.1.4 on Mac OS X

path='
/usr/local/bin 
/usr/local/sbin
/usr/local/lib
/usr/local/include
/usr/local/man
/usr/bin
/bin
/usr/sbin
/sbin
'

path="$(printf "%s" "${path// /}" | /usr/bin/tr '\n' ':' | /usr/bin/sed -E 's/^:|:$//g')"

printf "%s\n" "$path" | tr ':' '\n'

export PATH="${path}"


cd ~/Desktop

curl -L -O http://ftp.gnu.org/gnu/sed/sed-4.1.4.tar.gz
cd ~/Desktop/sed-4.1.4

# cf. Compiling GNU coreutils on Mac OS X, 
# http://codesnippets.joyent.com/posts/show/1799
./configure --prefix=/usr/local/gnucoreutils
  
make

/usr/bin/sudo /usr/bin/make install



ls -l /usr/local/gnucoreutils/bin/sed
/usr/local/gnucoreutils/bin/sed --version
man /usr/local/gnucoreutils/man/man1/sed.1

/usr/bin/sudo /bin/ln -is /usr/local/gnucoreutils/bin/sed /usr/local/bin/gnused

# use GNU sed with case insensitive option
echo Tool | sed 's/[Tt]/c/'
echo Tool | gnused 's/t/c/i'


# cf. "Extract title from HTML web page" on http://bosetsu.org/pub/docs/shell_cheatsheet.html
curl -L -s http://codesnippets.joyent.com/posts/show/1835 | \
     gnused -n 's/.*<[tT][iI][tT][lL][eE]>\(.*\)<\/[tT][iI][tT][lL][eE]>.*/\1/p;T;q'

# cf. http://www.pixelbeat.org/cmdline.html
curl -L -s http://codesnippets.joyent.com/posts/show/1835 | \
     gnused -n 's/.*<title>\(.*\)<\/title>.*/\1/ip;T;q'

Case sensitivity in PostgreSQL

When I started using PostgreSQL, it seemed odd that when I created a table called Users with column names like userId, userPassword, etc. When I would select these items from the table, the column and table names would all be in lowercase. It's quite simple really, I wasn't using quotes :) Below is an example of how to maintain your case in PostgreSQL:

This will all come out lowercase
create table Users (
  userId integer unique not null,
  userFirstName varchar(50) not null,
  userLastName varchar(50) not null
);


Whereas this will come out as you expect it to
create table "Users" (
  "userId" integer unique not null,
  "userFirstName" varchar(50) not null,
  "userLastName" varchar(50) not null
);