nix, shell, perl, php, mysql and mac os x tips and tricks

Tuesday, July 24, 2007

regex to grab the first n characters of a string, respecting words

^(.{0,MAXCHAR}[^ ,.]*)(.*)

Monday, July 23, 2007

Shell script to re-name first part of filename to uppercase, and the extension to lowercase

For example ck100.Jpg -> CK100.jpg. Also removes spaces. Must be run in the directory the files are located in.
for file in *; do myfile=`echo $file | sed -e 's/ //g'`; first=`echo $myfile | sed 's/^\(.*\)\.\(.*\)/\1/'|tr '[a-z]' '[A-Z]'`; ext=`echo $myfile | sed 's/^\(.*\)\.\(.*\)/\2/'|tr '[A-Z]' '[a-z]'`;  mv "$file" `echo "$first.$ext"`; done