Monday, May 20, 2013

Using awk to work on data files

In order to average numbers in a file we can use awk as so:
$ cat meyer-heavy.txt | awk '{ sum+=$1;count++ } END {print sum/count}'
To find the largest value:
$ cat meyer-heavy.txt | awk 'BEGIN{max=-9999} $1 > max {print $1; max=$1}' 
Improved version (only prints largest value, not intermediate ones): 
$ cat meyer-heavy.txt | awk 'BEGIN{max=-9999} $1 > max {max=$1} END{print max}'
To find the smallest value:
$ cat meyer-heavy.txt | awk 'BEGIN{min=9999} $1 < min { print $1; min=$1 }' 
Fixed version (doesn't assign blank lines as min value): 
$ cat meyer-heavy.txt | awk 'BEGIN{min=9999} /\-[0-9]+/&&$1 < min { min=$1 } END{print min}'
Where $1 is for column 1, use $2 for column 2 etc...

Thursday, May 16, 2013

Grep and recursion

Grep has for a long time had a recursive option "-r" but I have never been able to get it to do what I want because if you write
$ grep -r test *.txt
The *.txt part specifies the files to look in, and since the folders I want to recurse don't end with .txt it doesn't recurse into them.
The correct way to cause grep to recurse is to specify file patterns separately.
$ grep -r test --include "*.txt" ./

Friday, May 10, 2013

Getting the correct default paper size in LibreOffice Writer


For years OpenOffice/LibreOffice has ticked me off by endeavoring to give me documents that after I walk down the hall to the printer, are asking for A4 sized paper, though I live in the US where we use "Letter" sized paper.

Even though I set the defaults for LibreOffice, and even though the printer and any printer settings are ALL set to Letter.

Well, I found this documentation of what is a really stupid way to have to make this change actually happen. Create a template, and set it as "default" and I guess this works.


  1. Create a document and the content and formatting styles that you want.
  2. Choose File - Templates - Save.
  3. In the New Template box, type a name for the new template.
  4. In the Categories list, select "My Templates", and then click OK.
  5. Choose File - Templates - Organize.
  6. In the Templates list, double-click the "My Templates" folder.
  7. Right-click the template that you created, and choose Set as Default Template.
  8. Click Close.

Now if I can only get Impress to stop setting my default language as Finnish...

Tuesday, May 7, 2013

Git tutorial

While on the subject of danielmiessler.com, I noticed his GIT tutorial which is really easy to read:
http://danielmiessler.com/study/git/

Using tcpdump

One thing I can never seem to remember is how to get tcpdump to show entire packets.

The option is -s (for snaplength). So either -s 1514 or -s 0 will cause tcpdump to capture entire packets.

More good info on using tcpdump is at danielmiessler.com/study/tcpdump/.