Sunday, September 27, 2009

Rename files matching given spec to incrementing filenames

This one liner renames all files starting with shred_ to shred_0.tif, shred_1.tif etc.

ls shred_* | awk 'BEGIN{n=0;}{print "mv " $0 " shred_" n ".tif"; n++;}' | sh

Wednesday, September 23, 2009

My .mailcap

Used with mutt to view gifs and jpegs as ascii graphics!!!! requires aview package on ubuntu/debian:

image/gif; asciiview '%s'
image/png; asciiview '%s'
image/jpg; asciiview '%s'
image/jpeg; asciiview '%s'



Robbed off some random place on the internets.

Tuesday, September 22, 2009

Simple pysam example

This python code uses pysam to parse a bam file and print out all the alignment contained therein.

import pysam

# Open our sam file
samfile = pysam.Samfile()
samfile.open( "sim.bam", "rb" )

# Grab an alignment from the sam file

def m_fetch_callback( alignment ):
print str(alignment)

num_targets = samfile.getNumTargets()

for n in xrange(num_targets):
samfile.fetch(samfile.getTarget(n),m_fetch_callback)


Friday, September 18, 2009

Simple zlib file reading example

This C++ example can read from both compressed and compressed files. It will read from testfile.gz (which can either be compressed or uncompressed) and write the contents to the standard output (screen).


#include "zlib.h"

#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
# include <fcntl.h>
# include <io.h>
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
#else
# define SET_BINARY_MODE(file)
#endif

#include <iostream>

using namespace std;

int main(void) {

gzFile f = gzopen("testfile.gz","rb");

char buffer[1001];

for(;!gzeof(f);) {

int e = gzread(f,buffer,1000);
buffer[1000] = 0;
cout << buffer << endl;
}

gzclose(f);

}



Compile with g++ testprog.cpp -lz -o testprog

Count files matching a given type/find spec

Count file size in bytes of all files matching *fast4 modified in the last 90 days.

x=`find . -atime 90 -name \*fast4 -ls | awk '{print $7 " +"}' | xargs echo`; echo "$x 0" | bc

examine irssi scrollback

I always find this a pain to lookup. But to scrollback in irssi: esc then p, forward esc then n.

Wednesday, September 9, 2009

iWorks '09 Keynote hangs loading a ppt file potentially losing all your work

I had a problem. I'd been working all day on a presentation which I hadn't saved (yea I know). Then I tried to load a powerpoint file and Keynote just hung trying to load the file. If I couldn't get it to unhang I would have lost a days work as Keynote doesn't autosave. The solution is to attach gdb to the Keynote and get it to jump out of whichever loop it's stuck in. Like so:

news-macbook:MacOS new$ ps -ef | grep Keynote
501 13290 82 0 1:22.74 ?? 46:51.60 /Applications/iWork '09/Keynote.app/Contents/MacOS/Keynote -psn_0_1552763
news-macbook:MacOS new$ grep -p 13290
grep: invalid option -- p
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
news-macbook:MacOS new$ gdb -p 13290
GNU gdb 6.3.50-20050815 (Apple version gdb-1344) (Fri Jul 3 01:19:56 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".
/Applications/iWork '09/Keynote.app/Contents/MacOS/13290: No such file or directory
Attaching to process 13290.
Reading symbols for shared libraries . done
Reading symbols for shared libraries ................................................................................................................................................................................................................................ done
0x942ed26d in ___CFBasicHashFindBucket1 ()
(gdb) bt
#0 0x942ed26d in ___CFBasicHashFindBucket1 ()
#1 0x942edd5b in CFBasicHashFindBucket ()
#2 0x942edcd4 in -[NSClassicMapTable objectForKey:] ()
#3 0x942edca8 in NSMapGet ()
#4 0x008dcf77 in -[ADReference reference] ()
#5 0x008db594 in -[ADNode firstChildWithName:xmlNamespace:objcClass:resolveReference:] ()
#6 0x008db3d2 in -[ADNode firstChildWith:resolveReference:] ()
#7 0x008db369 in -[ADNode firstChildWith:] ()
#8 0x009036b6 in -[ADSStyleSheet(ADInternal) parentSheet] ()
#9 0x00903f22 in -[ADSStyleSheet(Private) recursiveStyleWithIdentifier:] ()
#10 0x00903613 in -[ADSStyleSheet ensureOwnStyleWithIdentifierExists:] ()
#11 0x005f50a7 in +[PIOGraphicStyleMapper mapMasterStylesFromPptSlide:toStyleSheet:withState:] ()
#12 0x005fbe13 in -[PIOMasterSlideMapper mapMasterStyles] ()
#13 0x005fbf9d in -[PIOMasterSlideMapper map] ()
#14 0x00610ea9 in -[PIOThemeMapper mapMaster:includingGraphics:] ()
#15 0x00610f94 in -[PIOThemeMapper mapMaster:] ()
#16 0x006111b1 in -[PIOThemeMapper mapMasters] ()
#17 0x00611285 in -[PIOThemeMapper map] ()
#18 0x005f28c1 in -[PIODocMapper(Private) mapThemes] ()
#19 0x005f238b in -[PIODocMapper map] ()
#20 0x005f9467 in -[PIOImporter readFromFile:binaryDirectory:templatePath:localizer:] ()
#21 0x0022e0e2 in ?? ()
#22 0x0022e2d0 in ?? ()
#23 0x0022f789 in ?? ()
#24 0x0022ecea in ?? ()
#25 0x000a8c5f in ?? ()
#26 0x000a875f in ?? ()
#27 0x96c08769 in -[NSDocument initWithContentsOfURL:ofType:error:] ()
#28 0x96c0830d in -[NSDocumentController makeDocumentWithContentsOfURL:ofType:error:] ()
#29 0x000163ed in ?? ()
#30 0x00096fb4 in ?? ()
#31 0x02ddac2e in -[SFAppApplicationDelegate(Private) pOpenDocumentWithContentsOfFile:] ()
#32 0x02dd822d in -[SFAppApplicationDelegate application:openFiles:] ()
#33 0x001f7f55 in ?? ()
#34 0x96c0637d in -[NSApplication(NSAppleEventHandling) _handleAEOpenDocumentsForURLs:] ()
#35 0x96b44104 in -[NSApplication(NSAppleEventHandling) _handleCoreEvent:withReplyEvent:] ()
#36 0x9432346c in -[NSAppleEventManager dispatchRawAppleEvent:withRawReply:handlerRefCon:] ()
#37 0x94323230 in _NSAppleEventManagerGenericHandler ()
#38 0x90905de6 in aeDispatchAppleEvent ()
#39 0x90905ce5 in dispatchEventAndSendReply ()
#40 0x90905bf2 in aeProcessAppleEvent ()
#41 0x9061a381 in AEProcessAppleEvent ()
#42 0x969bddd6 in _DPSNextEvent ()
#43 0x969bd40e in -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] ()
#44 0x9697f5fb in -[NSApplication run] ()
#45 0x0000f13f in ?? ()
#46 0x0000f0b8 in ?? ()
#47 0x0000ee50 in ?? ()
#48 0x0005cc79 in ?? ()
(gdb) ret
Make selected stack frame return now? (y or n) y


Keep 'ret'ing until you reach 'readFromFile'. At this point type 'c'. Keynote will then tell you it couldn't open the file and you will be able to save your existing document.

Thursday, September 3, 2009

Postfix won't start

Just noticed I wasn't receiving system mail for the last few weeks. Coming back after holiday. Anyway, one of the few services not to restart after the computer had been switched off in my absence was postfix.

Couldn't for the life of me work out the problem, it was complaining about not finding the host. Therefore I had a look in /etc/hosts and commented out the line below:

#
# hosts This file describes a number of hostname-to-address
# mappings for the TCP/IP subsystem. It is mostly
# used at boot time, when no name servers are running.
# On small systems, this file can be used instead of a
# "named" name server.
# Syntax:
#
# IP-Address Full-Qualified-Hostname Short-Hostname
#

127.0.0.1 localhost


# special IPv6 addresses
#::1 localhost ipv6-localhost ipv6-loopback

fe00::0 ipv6-localnet

ff00::0 ipv6-mcastprefix
ff02::1 ipv6-allnodes
ff02::2 ipv6-allrouters
ff02::3 ipv6-allhosts


The one about IPv6. Dunno what that was about - but it works now.