Wednesday, June 19, 2013

[Bash] text to html using vim built-in function TOhtml

#!/bin/bash
# convert a text file to html using vim built-in function TOhtml
# usage:
#            toHTML text-files
# http://scriptdemo.blogspot.com

if [ $# -eq 0 ]; then
   sed -n '3,4p' `which toHTML`
   exit
else
   for mytxt in $*
   do
      if [ -e ${mytxt} ]; then
         vim -c 'syntax on|TOhtml' -c 'wq|q!' ${mytxt}
      else
         echo "${mytxt} is not found"
      fi
   done
fi

Friday, May 3, 2013

[Matlab] remove the water bodies in a m_map figure

function m_nolakes(lakeColor)
% delete the water bodies patches in figures created by m_map
%           default lakeColor is white ([1 1 1])
% usage:
%           m_nolakes(lakeColor)
%           http://scriptdemo.blogspot.com

if nargin==0
   lakeColor=[1 1 1];
elseif nargin~=1
   help m_nolakes
   return
end

gshhstypes={'c','l','i','h','f'};
for nt=1:numel(gshhstypes)
    eval(['hw=findobj(gcf,''type'',''patch'',''tag'',''m_gshhs_',gshhstypes{nt},''',''facecolor'',lakeColor);']);
    if ~isempty(hw)
       delete(hw);
    end
end
hw=findobj(gcf,'type','patch','tag','m_coast','facecolor',lakeColor);
if ~isempty(hw)
   delete(hw);
end

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

ShowCalendar