Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8955807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:38:11+00:00 2026-06-15T14:38:11+00:00

I have a text file, each line of which contains two blank separated fields:

  • 0

I have a text file, each line of which contains two blank separated fields: x and y.

1 0
10 29
5 2

Now I would like to see the graph of y = f(x), where x and y are taken from the file.

I would prefer a curses picture in a terminal but it can be a picture in any graphics format as well.

What is the easiest way to do it in bash?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:38:12+00:00Added an answer on June 15, 2026 at 2:38 pm

    Here’s a very funny possibility. The thing I’m going to show you here is probably not what you’re looking for, it’s probably not optimized, it’s probably a whole bunch of junk, but it’s really funny. (I’m not expecting any upvotes for it).

    This script more or less plots a * at each point the coordinates of which are given in a file (given as first argument of the script). It computes the number of rows and columns of your terminal (using the command stty, if you happen to have it installed, it’s good, otherwise, well, it’s not going to work).

    #!/bin/bash
    
    isnumber() {
       for i; do
          [[ "$i" =~ [[:digit:]] ]] || return 1
          [[ "$i" =~ ^[+-]?[[:digit:]]*\.?[[:digit:]]*$ ]] || return 1
       done
       return 0
    }
    
    die() {
       echo >&2 "$@"
       exit 1
    }
    
    [[ -n $1 ]] || die "You must provide an argument"
    
    xarray=()
    yarray=()
    
    # Read file input
    while read x y _; do
       isnumber "$x" "$y" || continue
       xarray+=( "$x" )
       yarray+=( "$y" )
    done < "$1"
    
    # Check that we have at least one point
    (( ${#xarray[@]} )) || die "Error, there's no valid point inf file \`$1'"
    
    # Compute xmin, xmax, ymin and ymax:
    read xmin xmax ymin ymax valid < <(
       bc -l < <(
          echo "ymin=${yarray[0]}; ymax=${yarray[0]}"
          echo "xmin=${xarray[0]}; xmax=${xarray[0]}"
          for i in "${!xarray[@]}"; do
             echo "y=${yarray[i]}; if (ymax<y) ymax=y; if (ymin>y) ymin=y"
             echo "x=${xarray[i]}; if (xmax<x) xmax=x; if (xmin>x) xmin=x"
          done
          echo 'print xmin," ",xmax," ",ymin," ",ymax'
          # This will tell us if we have xmin<xmax and ymin<ymax
          echo 'print " ",(xmin<xmax)*(ymin<ymax)'
       )
    )
    
    # Check that xmin<xmax and ymin<ymax
    (( valid )) || die "Error, ! ( (xmin<xmax) && (ymin<ymax) )"
    
    # Get terminal's number of rows and columns
    IFS=' ;' read _ _ _ _ nbrows _ nbcols _ < <(stty -a)
    ((nbrows-=1))
    ((maxcols=nbcols-1))
    ((maxrows=nbrows-1))
    
    # Create an array full of spaces:
    points=()
    for ((i=0;i<nbrows*nbcols;++i)); do points+=( ' ' ); done
    
    # Put a '*' at each x y in array points
    while read r c; do
       printf -v X "%.f" "$c"
       printf -v Y "%.f" "$r"
       points[X+Y*nbcols]='*'
    done < <(
       bc -l < <(
          echo "xmin=$xmin; dx=$maxcols/($xmax-xmin)"
          echo "ymax=$ymax; dy=$maxrows/(ymax-($ymin))"
          for i in "${!xarray[@]}"; do
             echo "print (ymax-(${yarray[i]}))*dy,\" \",(${xarray[i]}-xmin)*dx,\"\n\""
          done
       )
    )
    
    # Now, print it! The clear is not mandatory    
    clear
    printf "%c" "${points[@]}"
    

    Call the script plot_file_in_terminal (or maybe a shorter name).

    You can try it, it’s very funny, with a Mandelbrot set: the following script generates an M-set (you can give the number of pixels for the x and y coordinates as input, well, figure out a few things by yourself):

    #!/bin/bash
    
    die() {
       echo >&2 "$@"
       exit 1
    }
    
    nbx=${1:-100}
    nby=${2:-100}
    Nmax=${3:-100}
    
    [[ $nbx =~ ^[[:digit:]]+$ ]] && ((nbx>5)) || die "First argument (nbx) must be an integer > 5"
    [[ $nby =~ ^[[:digit:]]+$ ]] && ((nby>5)) || die "Second argument (nby) must be an integer > 5"
    [[ $Nmax =~ ^[[:digit:]]+$ ]] && ((Nmax>5)) || die "Third argument (Nmax) must be an integer > 5"
    
    xmin=-1.5
    xmax=1
    ymin=-1
    ymax=1
    
    bc -l <<EOF
    for (k=0;k<$nbx;++k) {
       for (l=0;l<$nby;++l) {
          x0=k*($xmax-($xmin))/($nbx-1)+($xmin)
          y0=l*($ymax-($ymin))/($nby-1)+($ymin)
          x=x0
          y=y0
          isin=1
          for (i=0;i<$Nmax;++i) {
             if(x^2+y^2>1) {
                isin=0
                break
             }
             xn=x^2-y^2+x0
             y=2*x*y+y0
             x=xn
          }
          if(isin) print x0," ",y0,"\n"
       }
    }
    EOF
    

    Name it genMandelbrot and use it as ./genMandelbrot > Mset or ./genMandelbrot 100 50 > Mset if your terminal is more or less 100×50. Then:

    ./plot_file_in_terminal Mset
    

    Nice, eh?

    If you want a sine function (say from 0 to 2*pi):

    bc -l <<< "for (i=0;i<400;++i) { x=i*6.28/400; print x,\" \",s(x),\"\\n\" }" > sine
    

    Then call:

    ./plot_file_in_terminal sine
    

    Outputs

    Mandelbrot Set (in 80×24)

                                                            **
                                                        **********
                                                      ***********
                                                 *  *************** *   *
                                        *********************************** ****
                                        **************************************** *
                                    **********************************************
                                  **************************************************
             **************       **************************************************
           ******************** ****************************************************
     **  **************************************************************************
    ****************************************************************************
     **  **************************************************************************
           ******************** ****************************************************
             **************       **************************************************
                                  **************************************************
                                    **********************************************
                                        **************************************** *
                                        *********************************** ****
                                                 *  *************** *   *
                                                      ***********
                                                        **********
                                                            **
    gniourf@somewhere:~/cool_path$ 
    

    Sine (in 80×24)

                    *********
                 ****       ***
               ***             **
              **                 **
            **                    **
           **                       **
         ***                         **
        **                            **
       **                               **
      **                                 **
     **                                   **
    *                                      **                                      *
                                            **                                    **
                                             **                                 **
                                               **                              **
                                                **                            **
                                                 **                          **
                                                  ***                      **
                                                    **                    **
                                                     ***                ***
                                                       ***            ***
                                                         ***       ****
                                                            ********    
    gniourf@somewhere:~/an_even_cooler_path$ 
    

    In your OP you mentionned “the easiest way of doing”… well, you can notice, this solution it’s not exactly a one-liner.

    Edits.

    • Robustness: check that xmin!=xmax and ymin!=ymax so as to not divide by 0
    • Speed: Computing xmin, xmax, ymin, ymax in the same bc instance.
    • Speed: Don’t use tput anymore, it was retarded and too slow! Instead, build an array of points (each field contains a space or *).

    Todos.

    • Lots of stuff to have something we can actually work with confortably. This exercise is left to the reader.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file which contains a time stamp on each line. My
I have a text file with 50k+- lines and each line contains data which
I have a text file which contains many (over 5000) links. Like this: http://www.website.com/rgfdefvrggh
I have a text file that I would like to restructure. The file contains
I have a big text file which contains similar entries like this My goal
I have a text file where each line may end with some fixed TAG
If I have a text file with a separate command on each line how
I have a text file like this: A B C And each element has
I have a text file something like: lineat2 corrupt exitCode() and so on... Each
I have a single big text file in which I want to process each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.