LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   unix shell script to generate a bar graph (https://www.linuxquestions.org/questions/linux-newbie-8/unix-shell-script-to-generate-a-bar-graph-861737/)

pghosh06 02-09-2011 10:17 PM

unix shell script to generate a bar graph
 
Hello,I am a complete newbie in shell scripting.
I am working on a small project where I have tons of data to analyze.
I was wondering if it would be possible to write a script to generate a bar graph/chart/histogram etc that can make the analysis part simpler.

While dealing with so much data, i fear a manual approach could lead to human errors and hence thought that a script might be a better approach.

Please help.

MS3FGX 02-09-2011 10:36 PM

What exactly are you looking to do? Create a bar graph right in the terminal as the script runs, or create an image file that contains bar graphs representing the data?

pghosh06 02-09-2011 10:42 PM

I think creating an image file that contains bar graphs representing the data would really help.

A.Thyssen 02-09-2011 11:12 PM

First have a look at the simple script...
http://www.ict.griffith.edu.au/~anth...are/percent.sh

Another snippet that may be of interest...

Progress Bar

This is a all bash method (except for the sleep)

Code:

  bar="=================================================="
  barlength=${#bar}
  i=0
  while ((i < 100)); do
    n=$((i*barlength / 100))      # Number of bar segments to draw $((i/2))
    printf "\r[%-${barlength}s]" "${bar:0:n}"
    ((i += RANDOM%5+2))            # i = percentage done
    sleep 1
  done
  echo

Of course 'i' should be set from somewhere meaningful.

b0uncer 02-10-2011 03:01 AM

You can easily write a script to run R or Gnuplot, which can produce several kinds of graphs, including bar graphs, with various options. Personally I prefer Gnuplot over R for plotting, but the choice is yours (and presumably there are other alternatives as well). Either app is able to "work on" a file which contains the directives to do something (e.g. to do a bar graph, in this case), and that file you can create on the fly with a suitable script, which enables you to work on large amounts of data, if it's organized.

See the homepages of R and Gnuplot for more information, to begin with. First use them interactively (i.e. start either one, and then start doing things) to get to grips with them. Creating a bar chart is fairly easy, if your data is in a file organized so that the program is able to read it. When you know the (few) necessary things to work out the graph, put them into a file and have the program "run" that file. Once that works, write a small shell script, for example, that dynamically creates the file(s) (if you need more than one), and you have a solution.

Another approach: for example if you had N data files that had different names (but same structure) and you wanted N graphs out of them, you could create just one file for, say, Gnuplot that produced a bar graph from a given file. Then you didn't need to create several Gnuplot scripts/directive files, but one that worked on a certain data file, and in a (for example bash) script just link each real data file in turn to the name you used in the Gnuplot script. That's what I'd do, and just tell Gnuplot to take the necessary names etc. from the file (column names for example). Examples on how to create a bar graph are plenty (at least for R and Gnuplot), so you should be able to build one that you need quickly.

Edit: the programs can output a variety of formats, so you'll surely find something that you like. In addition they don't need graphical user interface for this, so you can run this easily in a shell, and then just get the results and look at them. If the data files are very large, this can take some time (depending on how you do the graph, but a file with millions of lines may take a moment to get through), but if done right, you don't need to supervise it when it's done.


All times are GMT -5. The time now is 11:06 PM.