Category Archives: Tutorials

AWK: Sum of a Column

Imagine you have a file with multiple columns and you need to quickly get the total of a column in command line in UNIX. The easiest way is to use AWK in the command line.

Let’s take the following file:

1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 10
3 4 5 6 7 8 9 11 12

To get the sum of column number 7 execute the following command:

awk '{sum+=$7} END {print sum}' datafile.txt

You will get the sum of 7, 8 and 9


Let’s now write a small script to calculate the sum of a column and also print a running total.


2010 191 291 391 491 591 691 791 891 991
2011 144 286 391 491 591 691 791 891 991
2012 112 254 354 454 554 654 754 854 954
2013 191 291 391 491 591 691 791 891 991
2014 191 291 391 491 591 691 791 891 991
2015 178 291 391 491 591 691 791 891 991


#!/bin/awk -f
# scriptname: calc_sum
# input: column number, data file
    printf ("\tValue\tRunning total\n")
    sum += $colnum;
    printf ("%s:\t%.2f\t%.2f\n", $1, $colnum, sum)
END { 
    printf ("Total:%.2f\n", sum)


$ calc_sum colnum=8 datafile.txt


        Value   Running total
2010:   791.00  791.00
2011:   791.00  1582.00
2012:   754.00  2336.00
2013:   791.00  3127.00
2014:   791.00  3918.00
2015:   791.00  4709.00
Total:  4709.00

Of course you can run the above program from the command line, but it is neater and readable this way.

Adding Code Formatting Tools in Notepad++

I am a big fan of beautiful code, not only in terms of quality of programming but also formatting the code nicely so that it is easy on eyes.

If you are using a full-blown IDE, they come with their own formatting facility, but if you are using a multipurpose editor such as Notepad++, then you may have to use a plugin to format your code. There are many 3rd party programs that can be used with Notepad++ that will help you format your source code without going through a lot of effort.

I use different tools for different languages. I use PerlTidy for formatting my Perl code, and AStyle for certain other languages. Here are the steps taken to add AStyle to Notepad++. Please check the AStyle website for further information on AStyle and its options.

First you need to make sure you have NppExec plugin added to your Notepad++ by going to Plugin menu.

NppExec Menu
NppExec Menu

Download AStyle. The zip archive includes all the source and an  executable. You can simple copy the executable to a suitable location (For this demo, I am copying it to H:\tools\AStyle.exe. Or you can recompile from the source.

Go to NppExec under Plugin menu and click on Execute or hit F6. You will get the Execute dialogue box.

Execute Dialogue Box
Execute Dialogue Box

Select ” from the dropdown box if it is not already selected.

Type in the following commands in the window:

h:\tools\AStyle --style=allman "$(FULL_CURRENT_PATH)"

The first line saves the file. If you do not save first, then it might format the last saved version of your file (Thanks arduinoparty for pointing out). The 2nd command formats the code and saves the file. The view doesn’t get refreshed, so we need to reload the file from disk, and npp_open does that in the above. Note that I am using AStyle with –style=allman flag which uses broken brackets.

Hit the save button and it will ask you for a name, just type an appropriate name and hit OK to run.

Execute AStyle
Execute AStyle

You could stop right there and every time you need to run the command, just go to Plugins -> NppExec -> Execute… But let’s go one step further.

Under Plugins, go to NppExec and click on Advanced Options.

NppExec Advanced Options
NppExec Advanced Options

Select “Run AStyle” from the Associated Script drop down menu and then hit the “Add/Modify” button. Make sure the “Place to the Macros Submenu” is ticked.

This will add a new option in Macro Menu.


Notepad++ Macro Menu

That is it. Hope you have found this short tutorial useful.

PCL – Centring Variable-width Text

If you ever had to deal with PCL you would know that there is no command to centre the text. There are some printers that support HPGL command embedded in the PCL file, but not all. If we are printing a predefined text, we know the measurements and would set absolute positioning. But if we are dealing with data generated from a data source, then how can we centre the text? One way to do it is to know the width of every character in every font-family and when printing, calculate the width of the text in whatever unit we are using and then position the text accordingly. The other way is to write an email to your manager and tell them that unless they move to PostScript, there is no way you can centre the text correctly.

But if you really want to do it without having to dramatically change your code or moving away from PCL, there is a trick that you might or might not have figured it out or heard of it.

The trick is to print transparent text upside down in half the point size from the centre of the page. What you will print next will start exactly from where the last one ended. Now, change rotation back to normal, and print your text in regular size in black. This will work very well.

  1. Choose transparent color [ESC]*vo1T
  2. Rotate 180 degrees [ESC]&a180P
  3. Drop the font size by half (assume your normal text size is 12) [ESC](s1p6v0s0b5T
  4. Print your text
  5. Choose black color [ESC]*v1oT
  6. Change rotation back to normal [ESC]&a0P
  7. Choose the normal font size [ESC](s1p12v0s0b5T
  8. Print your text.

Note: Please refer to your printer manual for specific PCL commands, such as font sequence.