Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 02-09-2011, 11:17 PM   #1
LQ Newbie
Registered: Feb 2011
Posts: 5

Rep: Reputation: 0
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.
Old 02-09-2011, 11:36 PM   #2
LQ Guru
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
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?
Old 02-09-2011, 11:42 PM   #3
LQ Newbie
Registered: Feb 2011
Posts: 5

Original Poster
Rep: Reputation: 0
I think creating an image file that contains bar graphs representing the data would really help.
Old 02-10-2011, 12:12 AM   #4
Registered: May 2006
Location: Brisbane, Australia
Distribution: linux
Posts: 153

Rep: Reputation: 44
First have a look at the simple script...

Another snippet that may be of interest...

Progress Bar

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

  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
Of course 'i' should be set from somewhere meaningful.
Old 02-10-2011, 04:01 AM   #5
LQ Guru
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
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.

Last edited by b0uncer; 02-10-2011 at 04:09 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Generate Items for a case shell script Snake_Eyes_ Linux - General 8 12-12-2009 07:56 AM
Generate Graph from iperf Statistics tassadaque Linux - Networking 1 08-20-2008 01:04 PM
Generate a random number from a bourne shell script lothario Linux - Software 2 03-02-2007 12:01 AM
Shell Script Progress bar rooch84 Linux - Software 8 09-03-2005 05:55 PM
PROGRESS BAR with shell script murugesan Linux - Software 5 12-13-2004 10:03 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:18 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration