Wednesday, July 15, 2009
converting postscript to an image
This is something I've meant to learn how to do for ages. I needed to do this to produce summaries for a shit load of PDFs (containing related molecules) and wanted to have an image of a structure to represent each PDF. Anyway, I print structures as postscript files, so I needed a way to create a PNG of the first structure for each PDF.
I saw a few solutions on line which used ghostview to first output a jpeg and then 'mogrify' to cut down the hunormous result to something useful. It turns out you can just do this:
This creates a png from the ps file. Blimey! Really simple!
The -trim tag is a great addition as well, since a direct conversion results in a lot of extra whitespace.
I saw a few solutions on line which used ghostview to first output a jpeg and then 'mogrify' to cut down the hunormous result to something useful. It turns out you can just do this:
convert molecule.ps -trim molecule.pngThis creates a png from the ps file. Blimey! Really simple!
The -trim tag is a great addition as well, since a direct conversion results in a lot of extra whitespace.
running command line perl within a bash script
Maybe it's just me, but this is something I've struggled with on a couple of occasions.
Since I can't give any of the examples I'm actually working on, I'll have to use something which is less obviously useful.
The simple version is to use bash to loop through some files and use Perl to print out the filename (as stored by the bash script):
The important things to make this work are:
1. The double quotes used for the Perl script. This enables the variable interpretation by bash.
2. Escape sequences for the double quotes used in the Perl script - the double quotes are necessary to make Perl interpret the Perl variables.
3. Escape sequences for the Perl variables - this distinguishes the Perl variables from the bash ones.
The final script I used was a lot more complicated than this and was used to generate a series of files. I guess I can put the script in as, with no context, it has little meaning.
It would have been nicer just to write a separate Perl script and call that, really. But there you go.
Since I can't give any of the examples I'm actually working on, I'll have to use something which is less obviously useful.
The simple version is to use bash to loop through some files and use Perl to print out the filename (as stored by the bash script):
for file in `ls *.label`;
do perl -le "\$a=1;print \"${file} \$a\"";
done
The important things to make this work are:
1. The double quotes used for the Perl script. This enables the variable interpretation by bash.
2. Escape sequences for the double quotes used in the Perl script - the double quotes are necessary to make Perl interpret the Perl variables.
3. Escape sequences for the Perl variables - this distinguishes the Perl variables from the bash ones.
The final script I used was a lot more complicated than this and was used to generate a series of files. I guess I can put the script in as, with no context, it has little meaning.
for si in `seq 38 47`;
do perl -F',' -lane "if(@F==4)
{
print \"\$F[0]\\t\$h{\$F[0]}:\$F[1],\$F[2]\"
if \$F[3] == $si and \!\$i{\$F[0]}++;
}
else{
@F=split/\t/;
\$h{\$F[1]} = \$F[0];
}" file1 file2 > output_${si}.tsv;
done
It would have been nicer just to write a separate Perl script and call that, really. But there you go.
Thursday, June 18, 2009
using find to catch up on what's changed
I came back from a two week vacation and needed to find out what had been updated/added/changed in that time in some shared project dirs. "find" was my friend. The following command gives a list of all files changed within the last two weeks.
find . -ctime -14'-ctime -14' will be true for all files/dirs that have been changed in the last 14*24 hours. You can alter the behaviour by changing the modifier on the numerical input:
+n for greater than n,
-n for less than n,
n for exactly n.
Monday, May 18, 2009
jvisualvm instead of kill -3
Whenever I need to see what's going on under the hood of a process I issue a kill -3 to get a stack-trace. Well, I was just introduced to jvisualvm! This is a great way to see what's going on with a running java process.
Changing the mysql prompt
This is such an obvious thing to do, why didn't I do it before?
I'm constantly jumping into and between multiple databases as multiple users (test, standard, mysql) it becomes a little dangerous and I'm finding myself checking where I am and who I am:
SELECT user();
SELECT database();
but you can change the standard mysql prompt to display the user and database! Just edit the my.cnf file and place the following under [mysql]:
Now my prompt looks like this:
paul@test:
or
root@main:
If you don't have permissions to alter the my.cnf directly, then you could change your personal .bashrc (or equivalent) to contain something like this:
This has saved me a lot of paranoia (and probably some headaches).
I'm constantly jumping into and between multiple databases as multiple users (test, standard, mysql) it becomes a little dangerous and I'm finding myself checking where I am and who I am:
SELECT user();
SELECT database();
but you can change the standard mysql prompt to display the user and database! Just edit the my.cnf file and place the following under [mysql]:
prompt=\\u@\\d:Now my prompt looks like this:
paul@test:
or
root@main:
If you don't have permissions to alter the my.cnf directly, then you could change your personal .bashrc (or equivalent) to contain something like this:
alias mysql='mysql --prompt="\u@\d:"'This has saved me a lot of paranoia (and probably some headaches).
Friday, May 1, 2009
bash substring substitution
I just came across this today, it's something that I wish I'd known years ago (I think it just shows that I need to read more manuals), I've been emulating this feature with Perl and regexes or with the basename/dirname commands.
Sweet, eh?
It's very simple; today I wanted to run through a set of files with a particular file extension, do some processing and then print the output to another file named the same as the original but with a different extension.
So, say the files are .tsv (tab delimited) and I want to create a .csv (comma delimited) one from the first two columns, you can do it like this:
for file in `ls *.tsv`;
do cut -f1,2 --output-delimiter=, \
$file > ${file/.tsv/.csv};
done
Subscribe to:
Posts (Atom)