Pretty printing json

Here is a really simple way to pretty print some unformatted json

$ echo '{"foo": "lorem", "bar": "ipsum"}' | python -mjson.tool  
    "bar": "ipsum",  
    "foo": "lorem"  
Posted in bash, python | Comments Off on Pretty printing json

Using awk to sum rows of numbers

I have a script which takes a tab-delmited file for regression tests, and converts it xml. I want to do a sanity check, to make sure that the number of utterances in my xml files matches the number in the tab-delimited.txt file. I can do this in 2 lines in UNIX

robert_felty$ wc -l samples2.txt  
72148 samples2.txt  
robert_felty$ find . -name '*.xml' | xargs grep -c "<utterance lang='pt-br'" | cut -f 2 -d ':' | awk ' { sum +=$1 } END { print sum }'  

In the first line, I count the number of lines (there is a heade line, so I will be expecting 1 fewer lines)

In the next line, I find all the .xml file using find, then pipe that to xargs, where I use “grep -c” to count the number of matches to the utternace pattern. grep -c outputs rows like this
I want to sum up all the counts, so I cut out just the count field using cut, then I use awk to sum up all the counts.

I love UNIX pipelines!

Posted in bash, linux, UNIX | Comments Off on Using awk to sum rows of numbers

Small fix for article class in plasTeX

I recently found a small error in plasTeX, the program I like to use to convert latex to html. Unfortunately, it looks like it is not being actively developed anymore, but since it is open-source, and it is written in python, which I know, I was able to figure out the issue in not too long. When running plasTeX, I was getting this error:

robert_felty$ plastex -c annualReport2012.cfg annualReport2012.tex
plasTeX version 0.9.1
ERROR: Could not load package "article": name 'Command' is not defined

I started googling around, and found
this post helpful. Basically it complains about not getting a proper stacktrace in the Base/LaTeX/ module. Indeed, I commented out the try/except clause in the PackageLoader class, and found that the problem was going all the way back to the Packages/ script.
I eventually figured out that article was importing stuff from report, which was importing stuff from book. I am not exactly sure why this wasn’t working, but importing directly from book, instead of vis-a-vis report seemed to do the trick.

 diff Packages/ Packages/
< from book import *
> from report import *
<     import book
<     book.ProcessOptions(options, document)
>     import report
>     report.ProcessOptions(options, document)

I haven’t encountered this issue before. I wonder if it has something to do with python 2.6 versus 2.7. I only use plasTeX about once a year to write my annual report, and I think I might have been on python 2.6 a year ago.

Posted in latex, python | Comments Off on Small fix for article class in plasTeX

Em dash in LaTeX

My friend Daniel Reeves was recently discussing different ways to typeset em dashes. Here is the way I like to do it in LaTeX.

GeSHi Error: GeSHi could not find the language tex (using path /home/robfelty/ (code 2)

Here is the resulting output:

Different ways to make em dashes in LaTeX

Different ways to make em dashes in LaTeX

Notice how using spaces around the --- can result in a dash at the end of a line, which is not desirable. And using no spaces around the --- doesn’t look nice, and can also result in problems when copying and pasting. The \dash command solves both of these issues. I did not come up with it myself, but I cannot remember where I found it.

Posted in latex | 1 Comment

Vetting vignetting

We recently got some family portraits taken at JCPenney. I think they turned out very nicely. They had a bunch of different effects that they could apply. While we were at the studio, I really liked the way that the vignetting effect brought out our faces. However, after investigating the full set (we bought the digital images), I decided I didn’t like the vignetting, because it was actually making my face a bit dark in one shot. Then I decided to play around with vignetting a bit more. I was disappointed to see that Picasa does not support vignetting. Fortunately I found a handy gimp script to do the job. At first I tried using it with a rectangular selection. Then I tried an oval one which I like best. Here are the various pictures:

Posted in linux, photography | 2 Comments