Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

LockeMcDonnell Group Title

C / Unix question, How to "sort" a printf?

  • one year ago
  • one year ago

  • This Question is Closed
  1. LockeMcDonnell Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Say I run a program, over the course of it's running, its prints a few lines. e.g. File1 40 File2 90 File3 20 How would I go about instead of printing it during its run time, "collect" the printed statements and then print them in sorted order. Should I use the unix command like: sort -k2n,2 -k1,1 myprogram

    • one year ago
  2. harshit1202 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    you can sort it using syntax " sort [options] filename " and using options -b : Ignores leading spaces in each line -d : Uses dictionary sort order. Conisders only spaces and alphanumeric characters in sorting -f : Uses case insensitive sorting. -M : Sorts based on months. Considers only first 3 letters as month. Eg: JAN, FEB -n : Uses numeric sorting -R : Sorts the input file randomly. -r : Reverse order sorting -k : Sorts file based on the data in the specified field positions. -u : Suppresses duplicate lines -t : input field separator

    • one year ago
  3. slotema Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    The Unix sort program works on files, not on programs. That's why `sort [options] myprogram` won't work. Instead, you'll need to tell sort to read from stdin (your terminal). You can do that by simply not giving a file name to sort (like `sort [options]`). But how can you use the output of `myprogram` as input for `sort`? That's where a pipe ('|') comes in. The pipe takes to output of the program before it and 'pipes' that output to the input of the program after it. So `./myprogram | sort -k2` will take the ouput from `./myprogram` and will use that output as input for `sort -k2`. That should give you the result you want. Another alternative is to write the ouput of myprogram to a file and run sort on that file. `./myprogram > tmp` will place the output of `./myprogram` in a file called `tmp`. Then you can sort that output with `sort -k2 tmp`. It'll work, but IMHO the first solution is better 'cause you're not constantly creating new files that you need to cleanup later.

    • one year ago
  4. LockeMcDonnell Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Hi thanks for your responses slotema and harpellet1202. slotema, where exactly would I put that statement? Since I don't want to printf my statements anywhere in my program except after I have sorted my output

    • one year ago
  5. LockeMcDonnell Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I like the first solution better to because like you said, I would have to create new files and delete them later

    • one year ago
  6. slotema Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    If you want to sort the output from inside your program, you'll need a different approach. You'll need to write a function that sorts the strings you want to print (there are different sorting algorithms you can use and adapt for that: http://en.wikipedia.org/wiki/Sorting_algorithm ). Or try to see if you can get your hands on the source code of the `sort` program.

    • one year ago
  7. LockeMcDonnell Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Just as a very basic idea of what I want to do: #include <stdio.h> void printThis(); int main(int argc, char *argv[]){ printf("blah3 30"); printThis(); return 0 ) void printThis(){ printf("blah2 20"); printf("blah1 10"); } So if I run this program, it would print out blah3 30 blah2 20 blah1 10 However what I want to do is instead of printing any of this, take all of what the program should have output, sort it using Unix sort, and THEN print it out blah1 10 blah2 20 blah3 30

    • one year ago
  8. LockeMcDonnell Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Ah, so I wouldn't be able to do this within the program right?

    • one year ago
  9. slotema Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    It's possible, but it'd take some effort to get it working properly. Another thing you can do is to write a shell script that runs your program and passes it to sort (like in my first response). You can run the shell script as if it was your program and you'll get the sorted output.

    • one year ago
  10. LockeMcDonnell Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    so would everything be encapsulated with a .sh file?

    • one year ago
  11. slotema Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    Yes, though if you want to you can leave out the '.sh' extension on a Unix system.

    • one year ago
  12. LockeMcDonnell Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    hmm

    • one year ago
  13. LockeMcDonnell Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I'll probably have to go the function route

    • one year ago
  14. LockeMcDonnell Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks a lot for your help slotema!

    • one year ago
  15. slotema Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    You're welcome. Good luck with the function route and just ask around if you need help with that.

    • one year ago
  16. LockeMcDonnell Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    thanks!

    • one year ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.