Sponsored Content
Top Forums Shell Programming and Scripting Fun with terminal plotting JSON data at the command line Post 303043053 by kbrazil on Thursday 16th of January 2020 12:33:22 PM
Old 01-16-2020
Fun with terminal plotting JSON data at the command line

One of the great thing about unix is the ability to pipe multiple programs together to manipulate data. Plain, unstructured text is the most common type of data that is passed between programs, but these days JSON is becoming more popular.

I thought it would be fun to pipe together some command line JSON tools to do some stupid terminal tricks, like plotting a graph of system statistics, like CPU or memory utilization.

We can use jc (disclosure: I wrote jc), jq, and jp to pull the output of uptime and display a line graph like this:

Image

In this post I'll show you how you can build a quick bar graph of the CPU utilization of the top processes right in the terminal. For more information on how to create the animated line graph above, see my blog post at blog.kellybrazil.com.

first, get jc, jq, and jp.

Then you can use this one-liner to graph the output of ps:
Code:
ps axu | jc --ps | jq '[.[] | select (.cpu_percent > 0.5)]' | jp -type bar -canvas full-escape -x ..pid -y ..cpu_percent

Image

Fun stuff! jq is an awesome JSON tool for the cli and is a bit like sed or awk for JSON. You'll find lots of uses for manipulating JSON as more and more programs start to output in the format.

Last edited by kbrazil; 01-16-2020 at 04:39 PM..
These 2 Users Gave Thanks to kbrazil For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

like to have fun in terminal

Hai Friends I have installed FreeBSD in my system... I have installed it to work in text mode don't have the GUI. The default text color is Black background with White Foreground. I want it to be with Black background with Green Foreground. How could i do that. Thanks in advance Collins (4 Replies)
Discussion started by: collins
4 Replies

2. UNIX for Advanced & Expert Users

Gnuplot question: plotting 3D data in map view

I have a simple gnuplot question. I have a set of points (list of x,y,z values; irregularly spaced, i.e. no grid) that I want to plot. I want the plot to look like this: - map view (no 3D view) - color of each point should depend on z-value. - I want to define my own color scale - plot should... (1 Reply)
Discussion started by: karman
1 Replies

3. UNIX for Dummies Questions & Answers

Plotting Data within UNIX

I have a set of data that looks similar to the following in UNIX: 0.12_0.008_fall_ff.out:bisect return: 0.08056640625 0.12_0.04_fall_ff.out:bisect return: 0.07470703125 0.12_0.12_fall_ff.out:bisect return: 0.06298828125 0.12_0.24_fall_ff.out:bisect return: 0.05126953125 Previously I have... (4 Replies)
Discussion started by: EDALBNUG
4 Replies

4. Shell Programming and Scripting

plotting a straight horizontal line

How can I plot a straight horizontal line using perl in unix solaris environment? Please suggest. Pooja (2 Replies)
Discussion started by: wadhwa.pooja
2 Replies

5. UNIX for Dummies Questions & Answers

Help with extracting data and plotting

I have attached a txt file, what I would like to be able to do is: 1. Extract Data from Columns labeled E/N and Ko into a new file 2. Then in the new file I would like to be able to plot E/N on the X axis and Ko on the y axis. 3. Lastly I would like to be able to extract multiple data sets and... (6 Replies)
Discussion started by: gingburg
6 Replies

6. Shell Programming and Scripting

Is command line invocation of gnome-terminal to run more than one command possible?

Hello, I am trying to learn how to pass something more than a one-command startup for gnome-terminal. I will give an example of what I'm trying to do here: #! /bin/bash # #TODO write this for gnome and xterm USAGE=" ______________________________________________ ${0##*/} run... (0 Replies)
Discussion started by: Narnie
0 Replies

7. UNIX Desktop Questions & Answers

How do you reverse terminal command line to the top?

Hi All, I work on a Linux platform which runs Red Hat (forget which version) and use both korn and bash shells. Is there a way of making the command line appear at the top of the terminal window and any lists, commands or directory names etc to appear below the top, that is to say reverse the... (1 Reply)
Discussion started by: ray_m
1 Replies

8. OS X (Apple)

How to start a new terminal from command line?

Dear All, Anyone knows how to start a new bash terminal from command line? Another question: when I use "open" command (open test.pdf) to open a pdf file, the PDF reader will start up, but cannot associate with that file. Anyone knows why? (1 Reply)
Discussion started by: andrewust
1 Replies

9. OS X (Apple)

A new OSX 10.12.x terminal from the command line.

Hi guys and gals... After much searching on the good ol' internet I could find nothing, so this is the result. ALthough many people seem to have asked this question no-one seems to have a solution so here we go. I need for AudioScope.sh, 'xterm' to run a second program for some of its... (2 Replies)
Discussion started by: wisecracker
2 Replies

10. Shell Programming and Scripting

Split JSON to different data files

Hi Gurus, I have below JSON file, now I want to rewrite this file into a new file. I will appreciate if anyone can help me to provide the solution...I can't use jq. { "_id": "3ad893cb4cf1560add7b4caffd4b6126", "_rev": "1-1f0ce165e1d210319cf6e9f9c6ff654f", "name":... (4 Replies)
Discussion started by: manas_ranjan
4 Replies
CGI::Application::Plugin::JSON(3pm)			User Contributed Perl Documentation		       CGI::Application::Plugin::JSON(3pm)

NAME
CGI::Application::Plugin::JSON - easy manipulation of JSON headers SYNOPSIS
use CGI::Application::Plugin::JSON ':all'; # add_json_header() is cumulative $self->add_json_header( foo => 'Lorem ipsum...'); $self->add_json_header( bar => [ 0, 2, 3, 4 ] ); $self->add_json_header( baz => { stuff => 1, more_stuff => 2 } ); # json_header() is not cumulative $self->json_header( foo => 'Lorem ipsum...'); # in case we're printing our own headers print "X-JSON: " . $self->json_header_string(); # clear out everything in the outgoing JSON headers $self->clear_json_header(); # or send the JSON in the document body $self->json_body( { foo => 'Lorem ipsum', bar => [ 0, 2, 3 ] } ); # send the JSON back in the document body, but execute it using a Javascript callback $self->json_callback('alert', { foo => 'Lorem ipsum', bar => [ 0, 2, 3 ] } ); DESCRIPTION
When communicating with client-side JavaScript, it is common to send data in "X-JSON" HTTP headers or through the document body as content- type "application/json". This plugin adds a couple of convenience methods to make that just a little bit easier. HEADER METHODS
json_header This method takes name-value pairs and sets them to be used in the outgoing JSON. It is not cummulative and works similarly to "header_props". Use it only if you have all of the values up front. In most cases add_json_header is probably what you want. # only the 2nd call will actually set data that will be sent $self->json_header( foo => 'Lorem ipsum...'); $self->json_header( bar => [ 0, 2, 3, 4 ] ); add_json_header This method takes name-value pairs and sets them to be used in the outgoing JSON. It is cummulative and works similarly to "header_add"; meaning multiple calls will add to the hash of outgoing values. # both 'foo' and 'bar' will exist in the hash sent out $self->json_header( foo => 'Lorem ipsum...'); $self->json_header( bar => [ 0, 2, 3, 4 ] ); clear_json_header This method will remove anything that was previously set by both json_header and add_json_header. This means that no "X-JSON" header will be sent. json_header_string This method will create the actual HTTP header string that will be sent to the browser. This plugin uses it internally to send the header, but it might be useful to use directly if you are printing your own HTTP headers (using a "header_type" of "none"). $self->header_type('none'); print $self->json_header_string(); json_header_value This method will return the values being sent in the JSON header. If you pass in the key of the value you want, you will get just that value. Else all name-value pairs will be returned. my $value = $self->json_header_value('foo'); my %values = $self->json_header_value(); BODY METHODS
json_body This method will take the given Perl structure, turn it into JSON, set the appropriate content-type, and then return the JSON. return $self->json_body({ foo => 'stuff', bar => [0,1,2,3]} ); json_callback This method will take the given Perl structure, turn it into JSON, set the appropriate content-type, and then return a Javascript snippet where the given callback is called with the resulting JSON. return $self->json_callback('alert', { foo => 'stuff', bar => [0,1,2,3]} ); # would result in something like the following being sent to the client alert({ foo => 'stuff', bar => [0,1,2,3]}); MISC METHODS
to_json This method is just a convenient wrapper around JSON::Any's "encode". from_json This method is just a convenient wrapper around JSON::Any's "decode". AUTHOR
Michael Peters, "<mpeters@plusthree.com>" BUGS
Please report any bugs or feature requests to "bug-cgi-application-plugin-viewsource@rt.cpan.org", or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Application-Plugin-JSON <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Application- Plugin-JSON>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. COPYRIGHT &; LICENSE Copyright 2006 Michael Peters, All Rights Reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.12.4 2011-11-11 CGI::Application::Plugin::JSON(3pm)
All times are GMT -4. The time now is 10:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy