Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 17
I have a text file that has coordinates points in it that need to be graphed. The first step i was going to do is to sort the points both ...
  1. #1
    Just Joined!
    Join Date
    Sep 2005
    Posts
    99

    sort command

    I have a text file that has coordinates points in it that need to be graphed. The first step i was going to do is to sort the points both by x and y to find the minimum and maximum of the points i Know the -g and -gr options find the max and min for the x values. Is there a way to delimit, either a ',' or ' ' character somehow with the sort function to find the y values max and min as they are on the same line as the x's. The input file looks like this:
    2 3
    0.2 .3
    .001 1.2
    5 6
    3.2 0.3
    5.8 6.1
    This is with spaces. I had comma's in there but changed them to spaces. Also, after the sort i have to take the first line and save it to a variable as that is the max or min for x that i need to set up the graph. Any help is appreciated. Thanks.

    ***EDIT***
    Don't know if this matters, but I am using bash

  2. #2
    Linux User
    Join Date
    Aug 2005
    Posts
    408
    This may be a really clumsy way to do this (and I'm pretty sure that someone must have a better way), but a quick and easy way to find the max and min of both X and Y is to use awk to print the columns individually and then pipe that to sort.

    So with your set of numbers as a sample.file, the following command:
    Code:
    awk '{print $1}' sample.file | sort
    produces the first column sorted:
    Code:
    .001
    0.2
    2
    3.2
    5
    5.8
    and the following command:
    Code:
    awk '{print $2}' sample.file | sort
    produces the second column sorted:
    Code:
    0.3
    1.2
    3
    .3
    6
    6.1
    If you have really long lists, you could use the head and tail commands to just spit out the first and last entries.

    You could also do other stuff with these entries. For instance, you could use head and tail to find the first entry of either one, and then you could send that entry to grep to find the line it appears on so that you could find the information it should be paired with (i.e., the corresponding x or y value). It would be easy then to assign that information to a variable once you have the command.

    Is that helpful?

  3. #3
    Just Joined!
    Join Date
    Sep 2005
    Posts
    99
    well i was thinking about it and this actually has to be read in from standard input which means the text file i am redirecting really should have X on first line Y on second X on third, Y on fourth and alternate like that until you dont want to enter anymore values. Then i was thinking of storing all of the X values in one file and all of the Y values in another file and sort them and take the first and last values and store them in maxX, minX, maxY, minY. So the head and tail command i think is more of what i need to know now to get those values and store them in those variables. Can i see an example of the head and tail commands? And then if I could just use the head command to get the first value and store that to a variable somehow and then use the tail command to get the last value and store that to a variable somehow. How would i store what the head command returns for the first line to a variable? Thanks.

  4. #4
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,789
    Quote Originally Posted by cwl157
    Can i see an example of the head and tail commands?
    head:

    Code:
    cat file | head -nX
    or
    Code:
    head -nX file
    will return the first X lines of the file "file". To get the last few lines, you just change head for tail eg:
    Code:
    cat file | head -nX
    or
    Code:
    tail -nX file
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

  5. #5
    Just Joined!
    Join Date
    Sep 2005
    Posts
    99
    cool this is what i have put together so far to input the x and y values and to get the max and min for each. I have 2 questions. One, to know when to end the asking for new points i use a blank line, however this messes up the sorting anyone know how i can ignore this first blank line from what i can tell the -b option of sort is not working. Other than the blank line problem it finds the min and max of y and the max of X correctly. Also, is .3 and 0.3 sorted the same and treated as the same number or is there a difference between those 2 numbers? Also, it finds these max and mins correctly which is good, however it displays it to the screen, how do i take them from the screen and store them in their respected variables? Any help is appreciated,
    Thanks.
    here is the code:
    Code:
    #!/usr/bin/bash
    
    touch Xpoints
    touch Ypoints
    varX=0
    while [ "$varX" != "" ]
    do
      echo "Enter an X value or a blank space to end input: "
      read varX;
      echo $varX >> Xpoints;
      if [ "$varX" == "" ]
      then
      break;
      fi
      echo "Enter a Y value: "
      read varY;
      echo $varY >> Ypoints;
    done
    
    #sort to find maxX, minX, maxY, minY
    minX=0.0
    minY=0.0
    maxX=0.0
    maxY=0.0
    sort -bg Xpoints > XpointsSorted
    head -n1 XpointsSorted
    tail -n1 XpointsSorted
    sort -bg Ypoints > YpointsSorted
    head -n1 YpointsSorted
    tail -n1 YpointsSorted

  6. #6
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,789
    Quote Originally Posted by cwl157
    how i can ignore this first blank line from what i can tell the -b option of sort is not working.
    Code:
    <snip>while [ "$varX" != "" ]
    do
      echo "Enter an X value or a blank space to end input: "
      read varX;
      if [ "$varX" == "" ]
      then
      break;
      else 
      echo $varX >> Xpoints;
      fi
      echo "Enter a Y value: "
      read varY;
      echo $varY >> Ypoints;
    done
    </snip>
    Note i moved the echo to within the if. so if varX is blank, break, otherwise echo it out to Xpoints. You may try the same thing for the Ypoints
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

  7. #7
    Just Joined!
    Join Date
    Sep 2005
    Posts
    99
    and what about taking the max and mins from the screen and storing them in the respected variables? Also, im not sure if the blank line is needed for the y points because you can never end on a y because each x needs a corresponding y so really you should only have the option to quit on an x value.

  8. #8
    Just Joined!
    Join Date
    Sep 2005
    Posts
    99
    Alright i cleaned it up a bit and figured out how to save the minX, maxX, minY, maxY to variables. Also, I want to store the x values and y values when they are initially read in into an array. So right now i have a variable for the x value and a variable for the y value. My idea is to get rid of these and replace them with arrays and then increment the array index each time through the loop. Since 2D arrays don't exist in bash i was thinking i could just have 1 array for X and one for Y. So it would be like arX[0] and arY[0] would be the first point entered. Anyone know how to do this in bash? Here is the updated code
    Code:
    !/usr/bin/bash
    
    touch Xpoints
    touch Ypoints
    varX=0
    while [ "$varX" != "" ]
    do
      echo "Enter an X value or a blank space to end input: "
      read varX;
      if [ "$varX" == "" ]
      then
      break;
      else
      echo $varX >> Xpoints;
      fi
      echo "Enter a Y value: "
      read varY;
      echo $varY >> Ypoints;
    done
    
    #sort to find maxX, minX, maxY, minY
    minX=`sort -g Xpoints | head -1`
    echo $minX
    maxX=`sort -g Xpoints | tail -1`
    echo $maxX
    minY=`sort -g Ypoints | head -1`
    echo $minY
    maxY=`sort -g Ypoints | tail -1`
    echo $maxY

  9. #9
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101
    Does this help....???

    Use sort command with -n and -k <column number> options (like in the example below)....I assumed 1st column is for X and 2nd is for Y:
    It will show smallest value for X(1st column) or Y(2nd column)...no. as provided to the -k <?> value.. See below:

    *** Note: If you also use -r option, the 1st line will show the highest value.

    [/E*Fare/Users/qabuild/aksutil/P/pank] $ cat eee
    10 10.1
    0.01 1.2
    .01 4
    9 .02
    9 9.1
    9.2 9.1
    9.4 9.2
    3 3.1
    8 5
    4 5.3
    2 5
    2.2 5.2

    [/E*Fare/Users/qabuild/aksutil/P/pank] $ cat eee|sort -n -k 1
    0.01 1.2
    .01 4
    2 5
    2.2 5.2
    3 3.1
    4 5.3
    8 5
    9 .02
    9 9.1
    9.2 9.1
    9.4 9.2
    10 10.1

    [/E*Fare/Users/qabuild/aksutil/P/pank] $ cat eee|sort -n -k 2
    9 .02
    0.01 1.2
    3 3.1
    .01 4
    2 5
    8 5
    2.2 5.2
    4 5.3
    9.2 9.1
    9 9.1
    9.4 9.2
    10 10.1

    Let me know, if this fails..
    Arun Sangal


    Quote Originally Posted by cwl157
    I have a text file that has coordinates points in it that need to be graphed. The first step i was going to do is to sort the points both by x and y to find the minimum and maximum of the points i Know the -g and -gr options find the max and min for the x values. Is there a way to delimit, either a ',' or ' ' character somehow with the sort function to find the y values max and min as they are on the same line as the x's. The input file looks like this:

    This is with spaces. I had comma's in there but changed them to spaces. Also, after the sort i have to take the first line and save it to a variable as that is the max or min for x that i need to set up the graph. Any help is appreciated. Thanks.

    ***EDIT***
    Don't know if this matters, but I am using bash
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

  10. #10
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    If you are going to use the shell to read each line, you can find the extrema as you are creating the file -- "is this value greater than any I have yet seen, if so, keep this, in any case append it to the file and read some more ...". That would eliminate the overhead of the sort, head, and tail calls.

    I would also make sure to keep the file around in case the user has made a typo -- he would not want to enter every piece of data again, but rather invoke an editor to make corrections ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...