Code Snippets

From Yossi Farjoun's Homepage
Revision as of 22:00, 30 May 2012 by Yossi (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

awk

Code Snippets/awk

Code Snippets/sed

Extracting attachments from an email file/directory

My problem:

I scanned many of my paper files using a scanner that emails me the resulting pdf. As a result I have many (many!) emails each with a 
pdf file attached. I wanted to extract all these attachments without doing so manually. 

My Specifics:

I use pine (so? shoot me!). so I can easily move all the email messages that I'm interested in extracting their attachments into one big "folder". 
This "folder" is actually one big computer file in a format called mbox (among many other names).

My solution: I wrote a python program (well, slightly modified one I found here). The program takes as input a directory name and a file name. The program extracts every attachment it finds in the mbox file and plops it into the directory. Very little error checking is done. If there are two attachments with the same given filename the second will over-write the first. I'm giving it here, with the regular caveats: It works for me. Your mileage may vary.

make a movie using mencoder

mencoder mf://*.png  -mf w=800:h=600:fps=25:type=png -ovc xvid  -xvidencopts pass=1:bitrate=250 -oac copy -o fig_art_geometry.avi

Tunneling through a firewall using ssh

tunnelling:

tunnel SSH port through lagrange to mathstation184

ssh -L8022:mathstation184:22 lagrange.mit.edu

now you have a port on your local machine 8022, that is hooked up to port 22 on mathstation184, so....

ssh -p 8022 localhost

if you wanted to rsync:

rsync -cav -e 'ssh -p 8022 localhost' localfiles localhost:remotefiles

Merging 2 pdf files

gs -q -sPAPERSIZE=letter -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=out.pdf in1.pdf in2.pdf ...

To print out a gif file with name as an annotation

f=<filename> convert -draw "text 10,20 ${f%%.gif}" $f -| lpr

thus, as a loop (in bash) you'd have

for f in *.gif; 
do convert -draw "text 10,20 ${f%%.gif}" $f -| lpr; 
done;

To do it for all gif files in directory

killall for solaris

killall is a nice command that is included in BSD. Solaris however doesn't always have it...here's a script that will emulate it:

   #!/bin/sh
   ps -a -o pid,fname | (read blah; while read pid name ; do 
   if  [ ${name} = ${1} ] ; then
       kill  ${pid}
   fi
   done)

To use, simply type

   % killall process-name-to-kill

and the script will attempt to kill all processes with that name. if you have no permission to do so, it will give an error.


UC Berkeley Airbears login

To login to the berkeley Airbears network without going through the gateway page (you will still an account and password) use the following script:

   #!/usr/bin/perl -w
   use strict;
   $|++;
   use File::Basename;
   use WWW::Mechanize 0.72;
   my $mech = WWW::Mechanize->new(  );
   $mech->get( "https://wireless-gw1.berkeley.edu/logon" );
   $mech->success() or die "Couldn't connect to wireless login page",
      $mech->response->status_line;
   # Select the form, fill the fields, and submit
   $mech->form_name( "logonForm" );
   $mech->set_visible([text=>"your-login-here"],[password=>"your-password-here"]);
   $mech->click("logon_action");
   $mech->success or die "Wireless login didn't seem to work.:,
       $mech->response->status_line;
   print "Logged onto Airbears.\n";

The script needs various libraries of perl, so make sure that you have them. Also make sure that you change the login and password to the correct values.


Using rsync to get a snapshot of my account at Berkeley

I use the following command locally, to get a current snapshot of my home directory at Berkeley copied to a directory on my laptop:

   % rsync avuzb --exclude '*~' --exclude '.*' yfarjoun@login.math.berkeley.edu:. yossi/mathhome/

Clearly, to use you need to replace the source and targets. Also, change the exclude directives if you prefer something else.


Roman numeral to Arabic decoder

This little Python script changes a Roman Numeral to a regular (arabic) number. It is quite robust, and will not check for errors in the input. It will also work "correctly" on mistakes such as iix (=8) and vl (=45) and such. It ignores characters that aren't in the list of 'ivxlcdm', they are considered 'whitespace' and the result is the sum of the different numbers in the input string. So that both 'i x' and 'iqx' translate to 11, (though clearly 'ix' translates to 9).

#!/opt/local/bin/python


value={'i':1,
       'v':5,
       'x':10,
       'l':50,
       'c':100,
       'd':500,
       'm':1000};
equiv={'iv':'IIII',
       'ix':'VIIII',
       'xl':'XXXX',
       'xc':'LXXXX',
       'cd':'CCCC',
       'cm':'DCCCC',
       'iix&':'VIII',
       'xxc':'LXXX',
       'ccm':'DCCC',
       'vl':'XXXXV',
       'ld':'CCCCL'};


def convert(roman):
    roman = roman.lower();
    for key in equiv.keys():
        # This little trick with the upper case is to avoid
        # double substitution as in ivx -> iiiix -> iiiviiii (which is wrong!)
        roman = roman.replace(key,equiv[key]);
    roman = roman.lower();
    sum = 0;
    for key in value.keys():
        sum+=roman.count(key)*value[key];
    return sum;


#if called from command line, use argv to find the string
#to convert and convert it.    
if __name__=='__main__':
    import sys
    print convert(sys.argv[1]);


Cute

econd month i second month i second month i second month i second month i second mo
using face her using face her using face her using face her using face her using fa
then stop tart then stop tart the stop start the stop start the stop start the stop
it at wasting pit at wasting pit a wasting spit a wasting spit a wasting spit a was
an hast your lean hast your lean has your clean has your clean has your clean has y
lead cars time lead cars time lead car time plead car time plead car time plead car
man and heat a man and heat a man and heat a ma and wheat a ma and wheat a ma and w
use over frogs use over frogs use over frogs use over frog use hover frog use hover
n a pore old and a pore old and a pore old and a pore old an a spore old an a spore
rm wiki rink farm wiki rink farm wiki rink farm wiki rink far wiki drink far wiki d
server my date server my date server my date server my date server my date server m
back ever fort back ever fort back ever fort back ever fort back ever fort back eve
this filler is this filler is this filler is this filler is this filler is this fil


(based on a stereogram design by Ray Butterworth)


Getting inline equations in Latex to look better

So that equation snippets don't get stretched together with the rest of your text in a latex document, you should surround it with a \mbox{ }. to do this automagically do a regexp replace (in emacs or whatever) replacing \(\$.*?\$\) with \\mbox{\1} if you already have some \mbox'es and want to avoid \mbox{\mbox{ }} you should use a more sophisticated regexp, but emacs doesn't allow lookahead and stuff. so I'm not sure how to do this.


Matlab quirks and work arounds

- when saving objects, careful when including pointers to functions. If these functions are nested functions or there are persistent variables around, the workspace will get saved as well (so that the functions can work...) even if the functions themselves do not need the variables.

- Matlab is using multi-threaded execusion and so if you want to time your runs (to know how your algorithem scales, you must start matlab with the option -singleCompThread as discussed here.

- When using Matlab with several screens, there is a strange java error that occurs when plotting. This can happen while editing a plot, or when plotting with sub-figures. At any rate, one workaround is to issue the command set(0,'DefaultFigureRenderer','OpenGL') as discussed here.

Personal tools