| #!/bin/bash # sum all/part the numbers in an array using bc # usage: # mysum number-array [index0 index1] # Note: the index starts from 1 # e.g., # myA=(34 23 32.5 33.2 25) # mysum "${myA[*]}" ==> 147.7 # mysum "${myA[*]}" 1 2 ==> 57 # mysum "${myA[*]}" 2 4 ==> 88.7 # mysum "12 23 12" ==> 47 # # http://scriptdemo.blogspot.com isDebug=0 if [ $# -eq 0 ]; then sed -n '3,8p' `which mysum` exit else myA=($1) [ ${isDebug} -eq 1 ] && echo ${myA[*]} [ ${isDebug} -eq 1 ] && echo "narg: $#" if [ $# -eq 1 ]; then if [ ${isDebug} -eq 1 ]; then echo "sum: `echo ${myA[*]} | sed 's/\ /+/g' | bc -l`" else echo "`echo ${myA[*]} | sed 's/\ /+/g' | bc -l`" fi elif [ $# -eq 2 ]; then ind0=`expr $2 - 1` nC=`expr ${#myA[*]} - ${ind0}` if [ ${isDebug} -eq 1 ]; then echo "sum: `echo ${myA[*]:${ind0}:${nC}} | sed 's/\ /+/g' | bc -l`" else echo "`echo ${myA[*]:${ind0}:${nC}} | sed 's/\ /+/g' | bc -l`" fi elif [ $# -eq 3 ]; then ind0=`expr $2 - 1` nC=`expr $3 - ${ind0}` if [ ${isDebug} -eq 1 ]; then echo "sum: `echo ${myA[*]:${ind0}:${nC}} | sed 's/\ /+/g' | bc -l`" else echo "`echo ${myA[*]:${ind0}:${nC}} | sed 's/\ /+/g' | bc -l`" fi fi |
Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts
Wednesday, August 6, 2014
[Bash] sum numbers in an array
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 |
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 |
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 } |
Friday, May 25, 2012
[Bash] send email with an attachment from Terminal
Enhanced version of back-up-text-files-using-mail-function
Also check mutt .
Also check mutt .
| #!/bin/bash # send email with a file as the attachment # usage: # mailit email files # http://scriptdemo.blogspot.com # note: # some old versions do not support the attachment option # [tested with Heirloom mailx 12.5] function isEmail() { cEmail=`echo $1 | grep "^[a-zA-Z]\+\([._][a-zA-Z0-9]\+\)*@\([a-zA-Z0-9]\+\.\)\+[a-zA-Z]\{2,3\}$"` if [ ${#cEmail} -gt 0 ]; then echo "true" else echo "false" fi } function isServerOn() { myS=$1 myp=`ping -c 2 ${myS} 2>&1 | grep "% packet" | awk '{print $6*100}'` if [ ${#myp} -eq 0 ]; then echo "-1" #server: ${myS} is not valid elif [ ${myp} -eq 100 ]; then echo "0" #server: ${myS} is off else echo "1" #server: ${myS} is on or partly responses fi } if [ $# -le 1 ]; then echo "usage: mailit email file1 [file2] ..." exit fi #check email address targetEmail=$1 isValidEmail=`isEmail ${targetEmail}` [ ${isValidEmail} == "false" ] && echo "${targetEmail} is not a valid email address!!!" && exit #check smtp server smtpS=`echo ${targetEmail} | awk -F\@ '{print $2}'` smtpS="smtp.${smtpS}" isOnline=`isServerOn ${smtpS}` if [ ${isOnline} == "-1" ]; then echo "SMTP server: ${smtpS} is invalid. STOP" && exit elif [ ${isOnline} == "0" ]; then echo "SMTP server: ${smtpS} is offline. STOP" && exit fi #send email for (( nf=2; nf<=$#; nf++ )) do eval "myfile=\$${nf}" if [ -f ${myfile} ]; then echo "The attachment is ${myfile} sent by `whoami` from `hostname`" > tmpcont.mailtomea mail -s "[script] ${myfile}" -a ${myfile} ${targetEmail} < tmpcont.mailtomea && \ echo "send mail successfully" && \ rm -f tmpcont.mailtomea else echo "${myfile} dos not exist" fi done |
Labels:
attachment,
bash,
email,
linux,
online,
script,
send email from terminal,
server,
validate email
Friday, May 18, 2012
[Bash] ris2bib: convert RIS citation file to bibtex format
| #!/bin/bash # convert ris format to bibtex format # usage: # ris2bib.sh ris-file(s) # www.scriptdemp.blogspot.ca getCTag() { for cTag in $* do nl=`grep "^${cTag}" ${ris} | wc -l` [ ${nl} -ne 0 ] && echo "${cTag}" && exit done echo "" } [ $# -eq 0 ] && sed -n '2,5p' `which ris2bib.sh` && exit for ris in $* do if [ -e ${ris} ]; then dos2unix ${ris} auTag=`getCTag AU A1 A2` tlTag=`getCTag T1 TI` joTag=`getCTag JO JA` yrTag=`getCTag PY Y1` doTag=`getCTag M3 DO` voTag="VL"; spTag="SP"; epTag="EP"; urTag="UR"; isTag="IS" publisher=`grep "^PB\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/^\ //g'` authorList0=`grep "^${auTag}\ " ${ris} | awk -F\- '{print $2"@"}'` authorList=`echo ${authorList0} | sed -e 's/^\ //g' -e 's/\.\ /\./g' -e "s/\@$//" | sed -e 's/@/\ and\ /g'` titleStr=`grep "^${tlTag}\ " ${ris} | cut -c7-` jourStr=`grep "^${joTag}\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/^\ //g'` volStr=`grep "^${voTag}\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/\ //g'` SPStr=`grep "^${spTag}\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/\ //g'` EPStr=`grep "^${epTag}\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/\ //g'` if [ ${#EPStr} -ne 0 ]; then pageStr="${SPStr}--${EPStr}" else pageStr=${SPStr} fi if [ ${#isTag} -ne 0 ]; then isnStr=`grep "^${isTag}\ " ${ris} | cut -c7-` fi yearStr=`grep "^${yrTag}\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/\ //g' | cut -c-4` if [ ${#doTag} -ne 0 ]; then doiStr=`grep "^${doTag}\ " ${ris} | awk -F\- '{print $2}' | rev | awk -F\: '{print $1}' | rev | sed -e 's/^\ //g'` fi if [ ${#urTag} -ne 0 ]; then urlStr=`grep "^${urTag}\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/^\ //g'` fi #generate the key mykey=`echo ${authorList} | awk -F\, '{print $1}'` mykey="${mykey}${yearStr}" titleKey=`echo ${titleStr} | awk '{print $1""$2}'` mykey=`echo "${mykey}${titleKey}" | awk '{print tolower($0)}' | sed -e 's/\-//g'` echo "" echo @article{"${mykey}," echo " author={${authorList}}," echo " title = {{${titleStr}}}," echo " journal = {${jourStr}}," echo " year = {${yearStr}}," echo " volume = {${volStr}}," [ ${#isnStr} -ne 0 ] && echo " number = {"${isnStr}"}," [ ${#doiStr} -ne 0 ] && echo " doi = {"${doiStr}"}," [ ${#urlStr} -ne 0 ] && echo " url = {${urlStr}}," if [ ${#publisher} -eq 0 ]; then echo " pages = {${pageStr}}" else echo " pages = {${pageStr}}," echo " publisher = {${publisher}}" fi echo " }" unset auTag tlTag joTag voTag spTag epTag isTag else echo "" echo "${ris} is not found!" echo "" fi done |
[Bash] convert agu citation to bibtex format
| #!/bin/bash # convert citation string from agu website to a bibtex format # usage: # agu2bib.sh 'citeString' # note: # 1: need single quotations # 2: the whole command should be in one line # www.scriptdemo.blogspot.ca getLastChar() { echo -n $* | tail -c1 } fixVolNum() { # get number from volume-string volstr=$1 if [ `getLastChar ${volstr}` == ")" ]; then numStr=`echo ${volstr} | awk -F\( '{print $2}' | rev | cut -c2- | rev` volstr=`echo ${numStr} | sed -e "s/\(${numStr}\)//"` echo "${volstr} ${numStr}" && exit else nf=`echo $volstr | awk -F\: '{print NF}'` if [ ${nf} -eq 2 ]; then echo ${volstr} | awk -F\: '{print $1" "$2}' && exit else echo "${volstr}" && exit fi fi } fixPageNum() { echo $1 | sed -e 's/–/--/g' | sed -e 's/----/--/' } revName() { nf=`echo $* | awk '{print NF}'` if [ $nf -ge 2 ]; then lastName=`echo $* | rev | awk '{print $1}' | rev` restName=`echo $* | sed -e "s/${lastName}//" | sed -e 's/\.\ /\./g' | sed -e 's/\ $//'` echo "${lastName}, ${restName}" else echo $* fi } getAuthor() { nf=`echo $* | awk --field-separator=" and " '{print NF}'` if [ ${nf} -eq 1 ]; then # one author # Myers, P. G. ==> Myers, P.G. echo $* | sed -e 's/\.\ /\./g' exit else # more than one author lastOne=`echo $* | awk --field-separator=" and " '{print $2}'` lastOne=`revName ${lastOne}` # how to get first one str0=`echo $* | awk --field-separator=" and " '{print $1}' | sed -e 's/\,$//'` nf=`echo ${str0} | awk -F\, '{print NF}'` if [ ${nf} -eq 1 ]; then echo "${str0} and ${lastOne}" exit fi fl2=`echo ${str0} | awk -F\, '{print $2}'` indNext=2 if [ `getLastChar ${fl2}` == "." ]; then # take first two fields as the first author fl1=`echo ${str0} | awk -F\, '{print $1}'` fl2=`echo ${fl2} | sed -e 's/\.\ /\./g' | sed -e 's/\ $//'` firstOne="${fl1}, ${fl2}" indNext=3 fi outStr=${firstOne} for (( n=${indNext}; n<=${nf}; n++)) do cfl=`echo ${str0} | cut -d\, -f ${n}` cnameStr=`revName ${cfl}` outStr="${outStr} and ${cnameStr}" done echo "${outStr} and ${lastOne}" && exit fi } [ $# -eq 0 ] && sed -n '2,7p' `which agu2bib.sh` && exit isDebug=0 nLen=4 inStr=$* nTLen=${#inStr} nLoop=`expr ${nTLen} - ${nLen} - 1` isFound=0 jTag="" for (( nL=1; nL<=${nLoop}; nL++ )) do subStr=${inStr:${nL}:${nLen}} # index starts from ZERO if [[ ${subStr} =~ "[1-2][0-9][0-9][0-9]" ]]; then [ ${isDebug} -eq 1 ] && set -x nL0=${nL} tmpChar=`echo ${inStr} | cut -c${nL0}` while [ "${tmpChar}" != ' ' ] do nL0=`expr ${nL0} - 1` tmpChar=`echo ${inStr} | cut -c${nL0}` done myStr0=`echo ${inStr} | cut -c-${nL0} | sed -e 's/\ $//g' -e 's/\,$//g'` nL1=`expr ${nL} + ${nLen} + 1` tmpChar=`echo ${inStr} | cut -c${nL1}` [ "${tmpChar}" == ":" ] && jTag="ams" while [ "${tmpChar}" != ' ' ] do [ "${tmpChar}" == ":" ] && jTag="ams" nL1=`expr ${nL1} + 1` tmpChar=`echo ${inStr} | cut -c${nL1}` done myStr1=`echo ${inStr} | cut -c${nL1}-` yearStr=${subStr} isFound=1 break fi done [ ${isDebug} -eq 1 ] && set +x [ ${isFound} -eq 0 ] && echo "can find the publicsh time!" && exit [ ${isDebug} -eq 1 ] && echo "second half: ${myStr1}" # get doi doi=`echo ${myStr1} | rev | awk -F, '{print $1}' | rev | sed -e 's/\ //g'` if [[ ${doi} =~ "doi*" ]] || [[ ${doi} =~ "DOI*" ]]; then isDOI=1 doi=`echo ${doi} | sed -e 's/\.$//'` pages=`echo ${myStr1} | rev | awk -F, '{print $2}' | rev | sed -e 's/\ //g'` vol=`echo ${myStr1} | rev | awk -F, '{print $3}' | rev | sed -e 's/\ //g'` if [ "${#jTag}" -eq 0 ]; then jour=`echo ${myStr1} | rev | awk -F, '{print $4}' | rev` fi else isDOI=0 pages=${doi} && unset doi pages=`echo ${pages} | sed -e 's/\.$//'` vol=`echo ${myStr1} | rev | awk -F, '{print $2}' | rev | sed -e 's/\ //g'` if [ "${#jTag}" -eq 0 ]; then jour=`echo ${myStr1} | rev | awk -F, '{print $3}' | rev` fi fi if [ ${#jTag} -eq 0 ]; then indOfJour=`awk -v a="${myStr1}" -v b="${jour}" 'BEGIN{print index(a,b)}'` indOfJour=`expr ${indOfJour} - 3` title=`echo ${myStr1} | cut -c-${indOfJour}` else title=`echo ${myStr1} | awk -F\. '{print $1}'` nL1=`expr ${#title} + 2` myStr1=`echo ${myStr1} | cut -c${nL1}-` jour=`echo ${myStr1} | awk -F\, '{print $1}'` nf=`echo ${myStr0} | awk -F\, '{print NF}'` if [ ${nf} -gt 2 ]; then myStr0=`echo ${myStr0} | rev | sed -e 's/\,/\ dna\ /' | rev` fi fi # get author string [ ${isDebug} -eq 1 ] && echo "first half: ${myStr0}" author=`getAuthor "${myStr0}"` pages=`fixPageNum ${pages}` volStr=`fixVolNum ${vol}` vol=`echo ${volStr} | awk '{print $1}'` num=`echo ${volStr} | awk '{print $2}'` #generate the key mykey=`echo ${author} | awk -F\, '{print $1}'` mykey="${mykey}${yearStr}" titleKey=`echo ${title} | awk '{print $1""$2}'` mykey=`echo "${mykey}${titleKey}" | awk '{print tolower($0)}' | sed -e 's/\-//g'` #output echo "@article{${mykey}," echo " author={${author}}," echo " title = {{${title}}}," echo " journal = {${jour}}," echo " year = {${yearStr}}," echo " volume = {${vol}}," [ ${#num} -ne 0 ] && echo " number = { ${num}}," if [ ${isDOI} -eq 1 ]; then echo " pages = {${pages}}," echo " doi = {${doi}}" else echo " pages = {${pages}}" fi echo "}" |
Example:
Output:
Subscribe to:
Posts (Atom)