Data processing with the Linux tools


The calculation also works with a database management system (DBMS), which is designed to support these kinds of tasks. Although you could use any database with an SQL query interface, I use PostgreSQL [11] in this example.

So-called aggregation functions [12]sum, min, max, and avg – are used by PostgreSQL to evaluate values. The invocation for this example would be:

SELECT SUM(number*distance) FROM drivinglog;

This time, drivinglog is not a file, but rather a database table. The terms number and distance refer to columns in the drivinglog table. Listing 5 shows the table entries, as well as the invocation for determining the total distance. PostgreSQL generates a results column called sum that contains the total distance as a single line.

Listing 5


database=> select * from drivinglog;
 number|       from_city   | to_city | distance
      1 | Berlin           | Potsdam |      30
      2 | Berlin           | Hamburg |     280
      2 | Frankfurt (Main) | Paris   |     575
(3 lines)
database=> select sum(number*distance) from drivinglog;
(1 line)


If you would rather click around than program, you can use a spreadsheet, such as Open/LibreOffice Calc [13], Gnumeric [14] from Gnome, or the standalone programs Pyspread [15] or Sc [16]. Calc even offers a suitable template free of charge [17].

Regardless of the spreadsheet, you'll need to enter the short formulas necessary for the calculations. These formulas allow you to calculate the number (of trips) multiplied by the distance, as well as the total of all intermediate values. In the example shown in Figure 2, the corresponding expression is sum(A2*D2,A3*D3,A4*D4).

Figure 2: A driving log in a Gnumeric spreadsheet.

The sum() function interprets the values inside brackets as a list of individual values. The entry A2 refers to the second row in column A of the current sheet.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Connecting a weather station to your Arduino

    After losing one weather station to tropical winds, the author reboots and designs a PCB that connects to an Arduino and monitors weather instruments.

  • Tracking airplanes in real time with ADS-B

    Airplanes continuously broadcast signals that identify the aircraft and its current flight path. With a moderately priced receiver and a Raspberry Pi, users can receive ADS-B transponder data in real time.

  • Graphical displays with Python and Pygame

    As its name implies, Pygame is a set of Python modules designed to write games. However, many Pygame modules are useful for any number of projects. We introduce you to a few Pygame modules that you can use to create custom graphical displays for your project.

  • A home intrusion detection setup (sort of)

    At least part of the popularity of the Raspberry Pi can be attributed to its high maker value; that is, a skilled maker with a Pi can build marvelous and beautiful things. Me? Not so much, but I was willing to try to build a home security system with the stuff in my junk box. Here's what happened …

  • Using a Raspberry Pi to make a hamster pedometer

    Researchers assert that hamsters run the equivalent of four marathons per night. We tested this with the help of a converted playback head from a video recorder, a hall sensor, and a Raspberry Pi.