Deprecated: (yfarjoun) preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /afs/athena.mit.edu/user/y/f/yfarjoun/web_scripts/homepage/includes/Sanitizer.php on line 1550
Code Snippets - Yossi Farjoun's Homepage

Code Snippets

From Yossi Farjoun's Homepage
(Difference between revisions)
Jump to: navigation, search
(New page: <h2>mass rename using sed</h2> for f in * ; do n=`echo $f | sed "s/\(.?*\) 1.m4a/\1.m4a/" `; mv "$f" "$n"; done; <h2>killall for solaris</h2> killall is a nice command that is include...)
 
Line 1: Line 1:
<h2>mass rename using sed</h2>
+
<h2>Mass rename using sed</h2>
  for f in * ; do n=`echo $f | sed "s/\(.?*\) 1.m4a/\1.m4a/" `; mv  "$f" "$n"; done;  
+
  for f in * ; do n=`echo $f | sed "s/\(.?*\) 1.m4a/\1.m4a/" `; mv  "$f" "$n"; done;
  
 +
<h2>To print out a gif file with name as an annotation</h2>
 +
 +
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
  
 
<h2>killall for solaris</h2>
 
<h2>killall for solaris</h2>

Revision as of 15:39, 22 January 2009

Contents

Mass rename using sed

for f in * ; do n=`echo $f | sed "s/\(.?*\) 1.m4a/\1.m4a/" `; mv  "$f" "$n"; done;

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.

Personal tools