Sunday, April 7, 2013

[Bash] extractBib: extract bib items from a source bibtex database for a given tex file

#!/bin/bash
# extract bib items from a source bibtex database for a given tex file
# usage:
#          extractBib tex-file bibtex-database output-bibtex-file
# e.g.,
#          extractBib mypaper.tex mybibtexdatabase.bib mypaper.bib
# http://scriptdemo.blogspot.com

function getkeys()
{
   texfile=$1
   mykeys=`grep "cite.*{.*}" ${texfile} | sed -e 's/\ //g' | awk '{for (i=1; i<=NF;i++) {if ($i ~ /cite/) {print $i}}}' | sed -e 's/\}/\n/g' | awk -F\{ '{print $2}' | sed -e 's/\,/\n/g' | sed '/^[\ ]*[\ ]*$/d' | sort | uniq | sed '/^\\\\/d' | sed '/:/d'` 
   echo ${mykeys}
}

if [ $# -eq 3 ]; then
   texfile=$1; bibfile=$2; outfile=$3
elif [ $# -eq 2 ]; then
   texfile=$1; bibfile=$2; outfile="extractbib_"$1
else
   echo "usage:"
   echo "          extractBib tex-file bibtex-database output-bibtex-file"
   exit
fi

# make sure no overwrite
[ -e ${outfile} ] && echo "${outfile} exists alreay! exit" && exit

allkeys=`getkeys ${texfile}`
if [ ${#allkeys[*]} -ne 0 ]; then
   touch ${outfile}
   for eachkey in ${allkeys[*]}
   do
       #echo ${eachkey}
       sed -ne "/${eachkey}/,/^[\ ]*\}$/p" ${bibfile} >> ${outfile}
   done
fi

Thursday, February 14, 2013

[Matlab] show a normal random heart

% Just for fun, not really useful....
% @ http://scriptdemo.blogspot.com 
clear; close all;
r=0.618; n=10000; re=sqrt(1-r*r);
x=normrnd(0,1,n,1);
y=x*r+normrnd(0,1,n,1)*re;y(x<0)=-y(x<0);
plot(x,y,'o','markerEdgeColor',[1 0 1],'markerFaceColor','none');
axis equal tight off;
set(gcf,'MenuBar','none','color','w')

Tuesday, November 20, 2012

[Bash] change current workspace by calling a script function

# to change current path using a customized script function
# instead of using alias. Need put this function in .bashrc
#    e.g.,
#            mycd ex1 ==> cd ~/scripttest/ex1
#            mycd ex2 ==> cd ~/scripttest/ex2
#            mycd eX2 ==> cd ~/scripttest/ex2
#   @ http://scriptdemo.blogspot.com 

function mycd (){
 if [ $# -ne 1 ]; then
    echo "Usage:"
    echo " mycd foldername"
 else
    targetF=`echo $1 | awk '{print tolower($1)}'`
    targetP=${HOME}/scripttest/${targetF}
    if [ -d ${targetP} ]; then
       eval "cd ${targetP}"
    else
       echo "${targetP} does not exist!"
    fi
    unset targetF
 fi
}

ShowCalendar