Tuesday, 12 January 2016

Quicker and dirtier one-line printing

In a previous post, I wrote about how to use a for loop and the system command to plot several data sets from different files in one go.  Taking that idea a bit further, one can make use of the dumb terminal type and a Unix pipe (assuming one is using a variety of Unix, such as OS X or Linux) to have a completely self-contained one-liner from a Unix shell.  This can be particularly useful for a quick way to check results running on a distant server that you want to connect to remotely via ssh and don't have the bandwidth to comfortably bring up graphical windows via X.

An example, building on the previous one, along with the result, is shown below:

% echo "set term dumb ; plot for [file in system('ls -1 */quadrupoles.res')] file u 1:4 w l t file" | gnuplot



  1200 ++---------+-----------+----------+----------+-----------+---------++
       *          +           +          +    66mev/quadrupoles.res ****** +
       *                                      69mev/quadrupoles.res ###### |
  1000 +*                                     70mev/quadrupoles.res $$$$$$++
       |*                                     71mev/quadrupoles.res %%%%%% |
       |#*                                                                 |
   800 ++*                                                                ++
       | *                                                                 |
       |  *                                                                |
   600 ++ #*                                                              ++
       |   *                              %                                |
       |   %*                         %%%%$$$$$$$$$$$$$$$$$$$$             |
       |    *                   %$############               $$$$$$$$$     |
   400 ++   #*               %##*******       ####                    $$$$++
       |     **          %******       ****      ##**                     $$
       |      **       ****                ***   **                        |
   200 ++      **    ***                      ***                         ++
       |        ** %**                                                     |
       +         ***          +          +          +           +          +
     0 ++---------+-----------+----------+----------+-----------+---------++
       0         100         200        300        400         500        600


Now, along with the plot for [file in ... construct, we have an extra gnuplot command set term dumb to make the output be character-based in the terminal window.  The Unix echo command prints the whole set of gnuplot instructions, and those are then piped to the gnuplot program using the Unix pipe symbol (|).

The dumb terminal by default uses a 'display' which is 79x24 characters, which is suitable for most default situations.  If you are running from a Terminal window in a windowing environment (the most common situation at the time of writing) then you can always drag your terminal to be much bigger and then request a more detailed plot.  The result is shown as a graphic below (unlike the above literal text result) so that it can be squashed down to fit in the blog post.  When used in anger, the font is just the same as the example above.  The difference in the command leading up to this figure is in writing set term dumb 300 75 to give 300 characters in the x direction and 75 in the y direction.

gnuplot textual plot