<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>andthennothing.net: Small scripts</title>
  <subtitle type="html">&amp;ldquo;first there was a three-legged monkey...&amp;rdquo;</subtitle>
  <id>tag:andthennothing.net,2005:Typo</id>
  <generator uri="http://typo.leetsoft.com" version="4.0">Typo</generator>
  <link href="http://andthennothing.net/xml/atom10/article/287/feed.xml" rel="self" type="application/xml+atom"/>
  <link href="http://andthennothing.net/archives/2005/10/11/small-scripts" rel="alternate" type="text/html"/>
  <updated>2005-12-18T03:19:08+00:00</updated>
  <entry>
    <author>
      <name>Jonas Bengtsson</name>
      <email>jonas.b@home.se</email>
    </author>
    <id>urn:uuid:68730605-5a82-457b-867a-26a357378c2c</id>
    <published>2005-10-11T21:24:00+00:00</published>
    <updated>2005-12-18T03:19:08+00:00</updated>
    <title>Small scripts</title>
    <link href="http://andthennothing.net/archives/2005/10/11/small-scripts" rel="alternate" type="text/html"/>
    <category term="script" scheme="http://andthennothing.net/tags/287"/>
    <category term="python" scheme="http://andthennothing.net/tags/287"/>
    <category term="perl" scheme="http://andthennothing.net/tags/287"/>
    <content type="html">&lt;p&gt;First I wrote a Perl stdin-parsing oneliner yesterday for summing up the output from &lt;code&gt;grep -c&lt;/code&gt;:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_perl "&gt;perl -e &amp;quot;my($sum) = 0; while(&amp;lt;STDIN&amp;gt;) { $sum += $1 if $_ =~ /^[^:]+:(\d+)$/ } print $sum;&amp;quot;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;And while the syntax is still a bit weird to me I&amp;#8217;m starting to like Perl a bit. &lt;em&gt;Did I just say that? I blame the Perlisms in Ruby!&lt;/em&gt; At least it solves the problems it&amp;#8217;s designed for in a good way.&lt;/p&gt;


	&lt;p&gt;I also needed a little script to replace all .html files on my old blog with a &amp;#8220;I&amp;#8217;ve moved&amp;#8221; notice. So I wrote the little Python script below (I added some error checking today to make it easier to use). It&amp;#8217;s super simple and hence a little embarrassing to post, but perhaps someone will need it some day (at least it will be easy for me to find). And that I wrote it in Python is a bit embarassing as well since I need to start using Ruby for these kinds of tasks.&lt;/p&gt;


	&lt;p&gt;Use at your own risk!&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_python "&gt;&amp;quot;&amp;quot;&amp;quot;Replaces all files in a directory (and its subdirectories) with one file.
Usage: replacefiles.py SOURCEFILE DESTINATIONDIRECTORY&amp;quot;&amp;quot;&amp;quot;

import sys, os.path, shutil

def replace_files_with_file(sourceFile, destinationDirectory, dryrun):
   os.path.walk(destinationDirectory, replace_directory, (dryrun, sourceFile))

def replace_directory(arg, dirname, names):
   dryrun, sourceFile = arg
   for name in names:
      name = os.path.join(dirname, name)
      if os.path.isdir(name):
         continue
      if dryrun:
         print &amp;quot;copy %(sourceFile)s %(name)s&amp;quot; % vars()
      else:
         shutil.copyfile(sourceFile, name)

def exit_print_help(help):
   print help
   sys.exit(-1)

if __name__ == '__main__':
   if len(sys.argv) != 3:
      exit_print_help(__doc__)
   script, source, destination = sys.argv
   if not (os.path.isfile(source) and os.path.isdir(destination)):
      exit_print_help(__doc__)
   print &amp;quot;First a dry run. The following would happen when replacing all files in '%(destination)s' with '%(source)s':&amp;quot; % vars()
   replace_files_with_file(source, destination, True)
   if raw_input(&amp;quot;Are you sure you want to do the actions listed above? [y/n] &amp;quot;)[0].lower() == 'y':
      replace_files_with_file(source, destination, False)
      print &amp;quot;Done!&amp;quot;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</content>
  </entry>
</feed>
